From 62c4dc2b68ea5f7f7226ebecd0704efb4c59feca Mon Sep 17 00:00:00 2001 From: PizzaMarinara Date: Mon, 26 Aug 2024 14:53:05 +0200 Subject: [PATCH 1/7] Changed layout of ButtonWidget --- .../android/widgets/button/ButtonWidget.kt | 8 +++-- .../res/drawable/rounded_image_background.xml | 5 +++ app/src/main/res/layout/widget_button.xml | 31 +++++++++---------- 3 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 app/src/main/res/drawable/rounded_image_background.xml diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt index 64d4b9a76a0..cb0f6cc809f 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt @@ -67,6 +67,7 @@ class ButtonWidget : AppWidgetProvider() { // Vector icon rendering resolution fallback (if we can't infer via AppWidgetManager for some reason) private const val DEFAULT_MAX_ICON_SIZE = 512 + private const val SMALL_MAX_ICON_SIZE = 80 } @Inject @@ -202,6 +203,9 @@ class ButtonWidget : AppWidgetProvider() { val icon = DrawableCompat.wrap(iconDrawable) if (widget?.backgroundType == WidgetBackgroundType.TRANSPARENT) { setInt(R.id.widgetImageButton, "setColorFilter", textColor) + setInt(R.id.widgetImageButton, "setBackgroundColor", Color.TRANSPARENT) + } else { + setInt(R.id.widgetImageButton, "setColorFilter", Color.TRANSPARENT) } // Determine reasonable dimensions for drawing vector icon as a bitmap @@ -210,11 +214,11 @@ class ButtonWidget : AppWidgetProvider() { val maxWidth = ( awo?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, DEFAULT_MAX_ICON_SIZE) ?: DEFAULT_MAX_ICON_SIZE - ).coerceAtLeast(16) + ).coerceAtLeast(16).coerceAtMost(SMALL_MAX_ICON_SIZE) val maxHeight = ( awo?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, DEFAULT_MAX_ICON_SIZE) ?: DEFAULT_MAX_ICON_SIZE - ).coerceAtLeast(16) + ).coerceAtLeast(16).coerceAtMost(SMALL_MAX_ICON_SIZE) val width: Int val height: Int if (maxWidth > maxHeight) { diff --git a/app/src/main/res/drawable/rounded_image_background.xml b/app/src/main/res/drawable/rounded_image_background.xml new file mode 100644 index 00000000000..bed23609ba1 --- /dev/null +++ b/app/src/main/res/drawable/rounded_image_background.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/widget_button.xml b/app/src/main/res/layout/widget_button.xml index 9eceab3128b..15a1b6a7fa1 100644 --- a/app/src/main/res/layout/widget_button.xml +++ b/app/src/main/res/layout/widget_button.xml @@ -10,44 +10,43 @@ android:background="@drawable/widget_button_background" android:padding="4dp"> - - + android:src="@drawable/ic_flash_on_24dp" + app:tint="@color/colorDynamicWidgetBackground"/> - + Date: Mon, 26 Aug 2024 15:45:01 +0200 Subject: [PATCH 2/7] Added customizable color to ButtonWidget icon --- .../android/widgets/button/ButtonWidget.kt | 11 +- .../button/ButtonWidgetConfigureActivity.kt | 5 + app/src/main/res/layout/widget_button.xml | 14 +- .../res/layout/widget_button_configure.xml | 22 + .../48.json | 1133 +++++++++++++++++ .../companion/android/database/AppDatabase.kt | 5 +- .../database/widget/ButtonWidgetEntity.kt | 2 + common/src/main/res/values/strings.xml | 2 + 8 files changed, 1188 insertions(+), 6 deletions(-) create mode 100644 common/schemas/io.homeassistant.companion.android.database.AppDatabase/48.json diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt index cb0f6cc809f..9014f951d3a 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt @@ -61,6 +61,7 @@ class ButtonWidget : AppWidgetProvider() { internal const val EXTRA_ACTION_DATA = "EXTRA_SERVICE_DATA" internal const val EXTRA_LABEL = "EXTRA_LABEL" internal const val EXTRA_ICON_NAME = "EXTRA_ICON_NAME" + internal const val EXTRA_ICON_COLOR = "EXTRA_ICON_COLOR" internal const val EXTRA_BACKGROUND_TYPE = "EXTRA_BACKGROUND_TYPE" internal const val EXTRA_TEXT_COLOR = "EXTRA_TEXT_COLOR" internal const val EXTRA_REQUIRE_AUTHENTICATION = "EXTRA_REQUIRE_AUTHENTICATION" @@ -232,6 +233,13 @@ class ButtonWidget : AppWidgetProvider() { // Render the icon into the Button's ImageView setImageViewBitmap(R.id.widgetImageButton, icon.toBitmap(width, height)) + widget?.iconColor?.let { + if(it.isNotBlank()) { + setInt(R.id.widgetImageButton, "setColorFilter", Color.parseColor(it)) + setInt(R.id.widgetImageButtonBackground, "setColorFilter", Color.parseColor(it)) + } + } + setOnClickPendingIntent( R.id.widgetImageButtonLayout, PendingIntent.getBroadcast( @@ -367,6 +375,7 @@ class ButtonWidget : AppWidgetProvider() { val label: String? = extras.getString(EXTRA_LABEL) val requireAuthentication: Boolean = extras.getBoolean(EXTRA_REQUIRE_AUTHENTICATION) val icon: String = extras.getString(EXTRA_ICON_NAME) ?: "mdi:flash" + val iconColor: String? = extras.getString(EXTRA_ICON_COLOR) val backgroundType = BundleCompat.getSerializable(extras, EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java) ?: WidgetBackgroundType.DAYNIGHT val textColor: String? = extras.getString(EXTRA_TEXT_COLOR) @@ -387,7 +396,7 @@ class ButtonWidget : AppWidgetProvider() { "label: " + label ) - val widget = ButtonWidgetEntity(appWidgetId, serverId, icon, domain, action, actionData, label, backgroundType, textColor, requireAuthentication) + val widget = ButtonWidgetEntity(appWidgetId, serverId, icon, domain, action, actionData, label, iconColor, backgroundType, textColor, requireAuthentication) buttonWidgetDao.add(widget) // It is the responsibility of the configuration activity to update the app widget diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt index f3a6bff9202..e23ec002fe9 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt @@ -239,6 +239,7 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() { val actionText = "${buttonWidget.domain}.${buttonWidget.service}" binding.widgetTextConfigService.setText(actionText) binding.label.setText(buttonWidget.label) + binding.iconColor.setText(buttonWidget.iconColor) binding.backgroundType.setSelection( when { @@ -454,6 +455,10 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() { ButtonWidget.EXTRA_ICON_NAME, binding.widgetConfigIconSelector.tag as String ) + intent.putExtra( + ButtonWidget.EXTRA_ICON_COLOR, + binding.iconColor.text.toString() + ) // Analyze and send action data val actionDataMap = HashMap() diff --git a/app/src/main/res/layout/widget_button.xml b/app/src/main/res/layout/widget_button.xml index 15a1b6a7fa1..d072cac2cb6 100644 --- a/app/src/main/res/layout/widget_button.xml +++ b/app/src/main/res/layout/widget_button.xml @@ -18,14 +18,22 @@ android:paddingHorizontal="4dp" android:orientation="vertical"> + + + android:src="@drawable/ic_flash_on_24dp"/> + + + + + + + Entity ID: Entity ID(s): Icon: + Icon color: Label: Action: Label @@ -1025,6 +1026,7 @@ Tap action: Toggle Unable to render the template + Color in hex format (eg. #FF0000) Label Entity ID Domain From 619b846594b10184f854c32135e5fef221e168e5 Mon Sep 17 00:00:00 2001 From: PizzaMarinara Date: Mon, 26 Aug 2024 16:14:03 +0200 Subject: [PATCH 3/7] Fixed ktlint issue --- .../companion/android/widgets/button/ButtonWidget.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt index 9014f951d3a..8b59939b3f0 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt @@ -234,7 +234,7 @@ class ButtonWidget : AppWidgetProvider() { setImageViewBitmap(R.id.widgetImageButton, icon.toBitmap(width, height)) widget?.iconColor?.let { - if(it.isNotBlank()) { + if (it.isNotBlank()) { setInt(R.id.widgetImageButton, "setColorFilter", Color.parseColor(it)) setInt(R.id.widgetImageButtonBackground, "setColorFilter", Color.parseColor(it)) } From 9b2ade0d8dfe1e6c56b51db71bec01509a040675 Mon Sep 17 00:00:00 2001 From: PizzaMarinara Date: Tue, 27 Aug 2024 12:12:25 +0200 Subject: [PATCH 4/7] Uniformed padding for the ButtonWidget component --- app/src/main/res/layout/widget_button.xml | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/app/src/main/res/layout/widget_button.xml b/app/src/main/res/layout/widget_button.xml index d072cac2cb6..e11b20d4c2c 100644 --- a/app/src/main/res/layout/widget_button.xml +++ b/app/src/main/res/layout/widget_button.xml @@ -1,21 +1,17 @@ + android:padding="8dp"> + android:layout_alignParentBottom="true"> Date: Tue, 27 Aug 2024 16:00:46 +0200 Subject: [PATCH 5/7] Validated the icon color text and fixed layout for different widget sizes --- .../android/widgets/button/ButtonWidget.kt | 15 ++++++++---- .../button/ButtonWidgetConfigureActivity.kt | 23 +++++++++++++++---- app/src/main/res/layout/widget_button.xml | 15 +++++++----- .../res/layout/widget_button_configure.xml | 3 +-- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt index 8b59939b3f0..e09dddf8d28 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt @@ -234,10 +234,7 @@ class ButtonWidget : AppWidgetProvider() { setImageViewBitmap(R.id.widgetImageButton, icon.toBitmap(width, height)) widget?.iconColor?.let { - if (it.isNotBlank()) { - setInt(R.id.widgetImageButton, "setColorFilter", Color.parseColor(it)) - setInt(R.id.widgetImageButtonBackground, "setColorFilter", Color.parseColor(it)) - } + setCustomColorToIcon(it, this) } setOnClickPendingIntent( @@ -406,4 +403,14 @@ class ButtonWidget : AppWidgetProvider() { onUpdate(context, AppWidgetManager.getInstance(context), intArrayOf(appWidgetId)) } } + + private fun setCustomColorToIcon(colorString: String, remoteViews: RemoteViews) { + val validColor = try { + Color.parseColor(colorString) + } catch (e: Exception) { + null + } + remoteViews.setInt(R.id.widgetImageButton, "setColorFilter", validColor ?: Color.TRANSPARENT) + remoteViews.setInt(R.id.widgetImageButtonBackground, "setColorFilter", validColor ?: Color.TRANSPARENT) + } } diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt index e23ec002fe9..1f9e4f762ea 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidgetConfigureActivity.kt @@ -5,6 +5,7 @@ import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.content.ComponentName import android.content.Intent +import android.graphics.Color import android.graphics.PorterDuff import android.graphics.PorterDuffColorFilter import android.os.Build @@ -437,6 +438,7 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() { val actions = actions[selectedServerId].orEmpty() val domain = actions[actionText]?.domain ?: actionText.split(".", limit = 2)[0] val action = actions[actionText]?.action ?: actionText.split(".", limit = 2)[1] + val iconColorText = validateIconColorText(binding.iconColor.text.toString()) intent.putExtra( ButtonWidget.EXTRA_DOMAIN, domain @@ -445,6 +447,10 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() { ButtonWidget.EXTRA_ACTION, action ) + intent.putExtra( + ButtonWidget.EXTRA_ICON_COLOR, + iconColorText + ) // Fetch and send label and icon intent.putExtra( @@ -455,10 +461,6 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() { ButtonWidget.EXTRA_ICON_NAME, binding.widgetConfigIconSelector.tag as String ) - intent.putExtra( - ButtonWidget.EXTRA_ICON_COLOR, - binding.iconColor.text.toString() - ) // Analyze and send action data val actionDataMap = HashMap() @@ -512,4 +514,17 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() { showAddWidgetError() } } + + private fun validateIconColorText(input: String): String { + return if (input.isEmpty()) { + input + } else { + try { + Color.parseColor(input) + input + } catch (e: Exception) { + "" + } + } + } } diff --git a/app/src/main/res/layout/widget_button.xml b/app/src/main/res/layout/widget_button.xml index e11b20d4c2c..3826c3c5495 100644 --- a/app/src/main/res/layout/widget_button.xml +++ b/app/src/main/res/layout/widget_button.xml @@ -18,9 +18,9 @@ android:id="@+id/widgetImageButtonBackground" android:layout_width="40dp" android:layout_height="40dp" + android:importantForAccessibility="no" android:translationZ="-2dp" android:alpha="0.4" - android:contentDescription="@string/widget_button_image_description" android:src="@drawable/rounded_image_background"/> + android:layout_alignParentBottom="true" + android:layout_below="@id/widgetImageButtonBackground" + android:gravity="bottom"> + android:textSize="20sp" + android:textStyle="bold" /> diff --git a/app/src/main/res/layout/widget_button_configure.xml b/app/src/main/res/layout/widget_button_configure.xml index b3af5448f32..91b9a40ae48 100644 --- a/app/src/main/res/layout/widget_button_configure.xml +++ b/app/src/main/res/layout/widget_button_configure.xml @@ -128,7 +128,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/widget_text_hint_icon_color" - android:imeOptions="actionDone" android:inputType="text" /> @@ -138,7 +137,7 @@ android:gravity="center" android:orientation="horizontal"> - Date: Tue, 27 Aug 2024 17:23:30 +0200 Subject: [PATCH 6/7] Minor layout fixes --- app/src/main/res/drawable/rounded_image_background.xml | 2 +- app/src/main/res/layout/widget_button.xml | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/res/drawable/rounded_image_background.xml b/app/src/main/res/drawable/rounded_image_background.xml index bed23609ba1..57e927724b0 100644 --- a/app/src/main/res/drawable/rounded_image_background.xml +++ b/app/src/main/res/drawable/rounded_image_background.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/app/src/main/res/layout/widget_button.xml b/app/src/main/res/layout/widget_button.xml index 3826c3c5495..2cf24937e84 100644 --- a/app/src/main/res/layout/widget_button.xml +++ b/app/src/main/res/layout/widget_button.xml @@ -6,7 +6,7 @@ android:minHeight="40dp" android:minWidth="40dp" android:background="@drawable/widget_button_background" - android:padding="8dp"> + android:padding="6dp"> + android:textSize="12sp" /> From 07fb97404064f3cd159a43c79574a3f2f1421265 Mon Sep 17 00:00:00 2001 From: PizzaMarinara Date: Mon, 2 Sep 2024 23:33:36 +0200 Subject: [PATCH 7/7] More UI fixes --- .../companion/android/widgets/button/ButtonWidget.kt | 10 +++------- app/src/main/res/drawable/rounded_image_background.xml | 2 +- app/src/main/res/layout/widget_button.xml | 7 ++++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt index e09dddf8d28..f3684310a98 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/button/ButtonWidget.kt @@ -202,11 +202,11 @@ class ButtonWidget : AppWidgetProvider() { size = IconicsSize.dp(24) } val icon = DrawableCompat.wrap(iconDrawable) + widget?.iconColor?.let { + setCustomColorToIcon(it, this) + } if (widget?.backgroundType == WidgetBackgroundType.TRANSPARENT) { - setInt(R.id.widgetImageButton, "setColorFilter", textColor) setInt(R.id.widgetImageButton, "setBackgroundColor", Color.TRANSPARENT) - } else { - setInt(R.id.widgetImageButton, "setColorFilter", Color.TRANSPARENT) } // Determine reasonable dimensions for drawing vector icon as a bitmap @@ -233,10 +233,6 @@ class ButtonWidget : AppWidgetProvider() { // Render the icon into the Button's ImageView setImageViewBitmap(R.id.widgetImageButton, icon.toBitmap(width, height)) - widget?.iconColor?.let { - setCustomColorToIcon(it, this) - } - setOnClickPendingIntent( R.id.widgetImageButtonLayout, PendingIntent.getBroadcast( diff --git a/app/src/main/res/drawable/rounded_image_background.xml b/app/src/main/res/drawable/rounded_image_background.xml index 57e927724b0..c2fc4aced4f 100644 --- a/app/src/main/res/drawable/rounded_image_background.xml +++ b/app/src/main/res/drawable/rounded_image_background.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/app/src/main/res/layout/widget_button.xml b/app/src/main/res/layout/widget_button.xml index 2cf24937e84..692c415be38 100644 --- a/app/src/main/res/layout/widget_button.xml +++ b/app/src/main/res/layout/widget_button.xml @@ -6,7 +6,7 @@ android:minHeight="40dp" android:minWidth="40dp" android:background="@drawable/widget_button_background" - android:padding="6dp"> + android:padding="8dp"> @@ -37,6 +36,7 @@ android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@id/widgetImageButtonBackground" + android:padding="2dp" android:gravity="bottom"> + android:textSize="16sp" + android:textStyle="bold" />