Add image utility methods

-Add convertCornerRadiusBitmap to edit bitmap cornor
-Add createIconWithDrawable to create icon from bitmap

Bug: 155822415
Test: manual test
Merged-In: Idb770ce5f3b9aaa1288538aa1e51ffc0d9f34a6e
Change-Id: Idb770ce5f3b9aaa1288538aa1e51ffc0d9f34a6e
(cherry picked from commit 37017a739c885ffd4aa450a4605dbeaaa2e4f7f3)
diff --git a/packages/SettingsLib/src/com/android/settingslib/Utils.java b/packages/SettingsLib/src/com/android/settingslib/Utils.java
index 9e59ce3..3cbf268 100644
--- a/packages/SettingsLib/src/com/android/settingslib/Utils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/Utils.java
@@ -13,6 +13,7 @@
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
+import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.ColorFilter;
 import android.graphics.ColorMatrix;
@@ -32,6 +33,10 @@
 import android.telephony.NetworkRegistrationInfo;
 import android.telephony.ServiceState;
 
+import androidx.annotation.NonNull;
+import androidx.core.graphics.drawable.RoundedBitmapDrawable;
+import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
+
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.UserIcons;
 import com.android.launcher3.icons.IconFactory;
@@ -537,4 +542,25 @@
                 == NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
         return !isInIwlan;
     }
+
+    /**
+     * Returns a bitmap with rounded corner.
+     *
+     * @param context application context.
+     * @param source bitmap to apply round corner.
+     * @param cornerRadius corner radius value.
+     */
+    public static Bitmap convertCornerRadiusBitmap(@NonNull Context context,
+            @NonNull Bitmap source, @NonNull float cornerRadius) {
+        final Bitmap roundedBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(),
+                Bitmap.Config.ARGB_8888);
+        final RoundedBitmapDrawable drawable =
+                RoundedBitmapDrawableFactory.create(context.getResources(), source);
+        drawable.setAntiAlias(true);
+        drawable.setCornerRadius(cornerRadius);
+        final Canvas canvas = new Canvas(roundedBitmap);
+        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
+        drawable.draw(canvas);
+        return roundedBitmap;
+    }
 }
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java
index 95e916b..df2f973 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java
@@ -18,6 +18,7 @@
 import android.util.Pair;
 
 import androidx.annotation.DrawableRes;
+import androidx.core.graphics.drawable.IconCompat;
 
 import com.android.settingslib.R;
 import com.android.settingslib.widget.AdaptiveIcon;
@@ -216,6 +217,23 @@
     }
 
     /**
+     * Create an Icon pointing to a drawable.
+     */
+    public static IconCompat createIconWithDrawable(Drawable drawable) {
+        Bitmap bitmap;
+        if (drawable instanceof BitmapDrawable) {
+            bitmap = ((BitmapDrawable) drawable).getBitmap();
+        } else {
+            final int width = drawable.getIntrinsicWidth();
+            final int height = drawable.getIntrinsicHeight();
+            bitmap = createBitmap(drawable,
+                    width > 0 ? width : 1,
+                    height > 0 ? height : 1);
+        }
+        return IconCompat.createWithBitmap(bitmap);
+    }
+
+    /**
      * Build device icon with advanced outline
      */
     public static Drawable buildAdvancedDrawable(Context context, Drawable drawable) {