From 2466e297bea40a6d4a452599c1188410e2ab0f5a Mon Sep 17 00:00:00 2001 From: Philoul Date: Sat, 26 Aug 2023 23:01:08 +0200 Subject: [PATCH] Wear cwf Include svg format for vector images (background, cover_plate, ...) --- app-wear-shared/shared/build.gradle | 1 + .../rx/weardata/CustomWatchfaceFormat.kt | 18 ++++++++++++------ build.gradle | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app-wear-shared/shared/build.gradle b/app-wear-shared/shared/build.gradle index 233b7dd1e1..c4ecc5fa7f 100644 --- a/app-wear-shared/shared/build.gradle +++ b/app-wear-shared/shared/build.gradle @@ -35,6 +35,7 @@ dependencies { api "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinx_serialization_version" api "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$kotlinx_serialization_version" + api "com.caverock:androidsvg:$caverock_androidsvg_version" api "org.apache.commons:commons-lang3:$commonslang3_version" //RxBus diff --git a/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt b/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt index 3718297096..6d1a8e4ad5 100644 --- a/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt +++ b/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt @@ -4,12 +4,15 @@ import android.content.res.Resources import android.graphics.BitmapFactory import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable +import android.graphics.drawable.PictureDrawable import androidx.annotation.DrawableRes import androidx.annotation.StringRes +import com.caverock.androidsvg.SVG import info.nightscout.shared.R import kotlinx.serialization.Serializable import org.json.JSONObject import java.io.BufferedOutputStream +import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.File import java.io.FileOutputStream @@ -44,7 +47,7 @@ enum class DrawableFormat(val extension: String) { UNKNOWN(""), //XML("xml"), - //SVG("svg"), + SVG("svg"), JPG("jpg"), PNG("png"); @@ -66,11 +69,14 @@ data class DrawableData(val value: ByteArray, val format: DrawableFormat) { val bitmap = BitmapFactory.decodeByteArray(value, 0, value.size) BitmapDrawable(resources, bitmap) } - /* - DrawableFormat.SVG -> { - //TODO: include svg to Drawable convertor here - null - } + DrawableFormat.SVG -> { + val svg = SVG.getFromInputStream(ByteArrayInputStream(value)) + val picture = svg.renderToPicture() + PictureDrawable(picture).apply { + setBounds(0, 0, svg.documentWidth.toInt(), svg.documentHeight.toInt()) + } + } + /* DrawableFormat.XML -> { // Always return a null Drawable, even if xml file is a valid xml vector file val xmlInputStream = ByteArrayInputStream(value) diff --git a/build.gradle b/build.gradle index 9fca843e97..891e41e5af 100644 --- a/build.gradle +++ b/build.gradle @@ -51,6 +51,8 @@ buildscript { kotlinx_datetime_version = '0.4.0' kotlinx_serialization_version = '1.5.1' + + caverock_androidsvg_version = '1.4' } repositories { google()