Skip to content

Commit

Permalink
add transparent background to media widget (#4618)
Browse files Browse the repository at this point in the history
* add transparent background support to media widget

Allow users to configure the media widget to have a transparent background.

* add helper to create widget background option adapter

remove duplicated logic to check for dynamic colour support and when displayed the currently selected value.

* Update import ordering
  • Loading branch information
ShaunPlummer authored Sep 10, 2024
1 parent ece89d4 commit b6a9934
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.google.android.material.color.DynamicColors
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
Expand All @@ -49,6 +48,7 @@ import io.homeassistant.companion.android.widgets.BaseWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.common.ActionFieldBinder
import io.homeassistant.companion.android.widgets.common.SingleItemArrayAdapter
import io.homeassistant.companion.android.widgets.common.WidgetDynamicFieldAdapter
import io.homeassistant.companion.android.widgets.common.WidgetUtils
import javax.inject.Inject
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -225,30 +225,19 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() {
}

val buttonWidget = buttonWidgetDao.get(appWidgetId)

val backgroundTypeValues = mutableListOf(
getString(commonR.string.widget_background_type_daynight),
getString(commonR.string.widget_background_type_transparent)
)
if (DynamicColors.isDynamicColorAvailable()) {
backgroundTypeValues.add(0, getString(commonR.string.widget_background_type_dynamiccolor))
}
val backgroundTypeValues = WidgetUtils.getBackgroundOptionList(this)
binding.backgroundType.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, backgroundTypeValues)

if (buttonWidget != null) {
val actionText = "${buttonWidget.domain}.${buttonWidget.service}"
binding.widgetTextConfigService.setText(actionText)
binding.label.setText(buttonWidget.label)

binding.backgroundType.setSelection(
when {
buttonWidget.backgroundType == WidgetBackgroundType.DYNAMICCOLOR && DynamicColors.isDynamicColorAvailable() ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_dynamiccolor))
buttonWidget.backgroundType == WidgetBackgroundType.TRANSPARENT ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_transparent))
else ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_daynight))
}
WidgetUtils.getSelectedBackgroundOption(
this,
buttonWidget.backgroundType,
backgroundTypeValues
)
)
binding.textColor.isVisible = buttonWidget.backgroundType == WidgetBackgroundType.TRANSPARENT
binding.textColorWhite.isChecked =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.homeassistant.companion.android.widgets.common

import android.content.Context
import com.google.android.material.color.DynamicColors
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.database.widget.WidgetBackgroundType

/**
* Shared helpers for working with widgets.
*/
object WidgetUtils {

/**
* Create an adapter for the list of background colour options for a widget.
*/
fun getBackgroundOptionList(context: Context): Array<String> {
val backgroundTypeValues = mutableListOf(
context.getString(R.string.widget_background_type_daynight),
context.getString(R.string.widget_background_type_transparent)
)
if (DynamicColors.isDynamicColorAvailable()) {
backgroundTypeValues.add(0, context.getString(R.string.widget_background_type_dynamiccolor))
}
return backgroundTypeValues.toTypedArray()
}

fun getSelectedBackgroundOption(context: Context, selectedType: WidgetBackgroundType, options: Array<String>) = when {
selectedType == WidgetBackgroundType.DYNAMICCOLOR && DynamicColors.isDynamicColorAvailable() ->
options.indexOf(context.getString(R.string.widget_background_type_dynamiccolor))
selectedType == WidgetBackgroundType.TRANSPARENT ->
options.indexOf(context.getString(R.string.widget_background_type_transparent))
else ->
options.indexOf(context.getString(R.string.widget_background_type_daynight))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import androidx.core.content.getSystemService
import androidx.core.graphics.toColorInt
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.integration.Entity
Expand All @@ -38,6 +37,7 @@ import io.homeassistant.companion.android.util.getHexForColor
import io.homeassistant.companion.android.widgets.BaseWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.BaseWidgetProvider
import io.homeassistant.companion.android.widgets.common.SingleItemArrayAdapter
import io.homeassistant.companion.android.widgets.common.WidgetUtils
import javax.inject.Inject
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -131,14 +131,7 @@ class EntityWidgetConfigureActivity : BaseWidgetConfigureActivity() {

val tapActionValues = listOf(getString(commonR.string.widget_tap_action_toggle), getString(commonR.string.refresh))
binding.tapActionList.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, tapActionValues)

val backgroundTypeValues = mutableListOf(
getString(commonR.string.widget_background_type_daynight),
getString(commonR.string.widget_background_type_transparent)
)
if (DynamicColors.isDynamicColorAvailable()) {
backgroundTypeValues.add(0, getString(commonR.string.widget_background_type_dynamiccolor))
}
val backgroundTypeValues = WidgetUtils.getBackgroundOptionList(this)
binding.backgroundType.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, backgroundTypeValues)

if (staticWidget != null) {
Expand Down Expand Up @@ -175,17 +168,6 @@ class EntityWidgetConfigureActivity : BaseWidgetConfigureActivity() {
val toggleable = entity?.domain in EntityExt.APP_PRESS_ACTION_DOMAINS
binding.tapAction.isVisible = toggleable
binding.tapActionList.setSelection(if (toggleable && staticWidget.tapAction == WidgetTapAction.TOGGLE) 0 else 1)

binding.backgroundType.setSelection(
when {
staticWidget.backgroundType == WidgetBackgroundType.DYNAMICCOLOR && DynamicColors.isDynamicColorAvailable() ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_dynamiccolor))
staticWidget.backgroundType == WidgetBackgroundType.TRANSPARENT ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_transparent))
else ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_daynight))
}
)
binding.textColor.visibility = if (staticWidget.backgroundType == WidgetBackgroundType.TRANSPARENT) View.VISIBLE else View.GONE
binding.textColorWhite.isChecked =
staticWidget.textColor?.let { it.toColorInt() == ContextCompat.getColor(this, android.R.color.white) } ?: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -175,6 +176,10 @@ class MediaPlayerControlsWidget : BaseWidgetProvider() {
val album = entity?.attributes?.get("media_album_name")?.toString()
val icon = entity?.attributes?.get("icon")?.toString()

if (widget.backgroundType == WidgetBackgroundType.TRANSPARENT) {
setInt(R.id.widgetLayout, "setBackgroundColor", Color.TRANSPARENT)
}

if ((artist != null || album != null) && title != null) {
setTextViewText(
R.id.widgetMediaInfoArtist,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import android.widget.Spinner
import android.widget.Toast
import androidx.core.content.getSystemService
import androidx.lifecycle.lifecycleScope
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.integration.Entity
Expand All @@ -26,6 +25,7 @@ import io.homeassistant.companion.android.databinding.WidgetMediaControlsConfigu
import io.homeassistant.companion.android.settings.widgets.ManageWidgetsViewModel
import io.homeassistant.companion.android.widgets.BaseWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.common.SingleItemArrayAdapter
import io.homeassistant.companion.android.widgets.common.WidgetUtils
import java.util.LinkedList
import javax.inject.Inject
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -117,25 +117,22 @@ class MediaPlayerControlsWidgetConfigureActivity : BaseWidgetConfigureActivity()

val mediaPlayerWidget = mediaPlayerControlsWidgetDao.get(appWidgetId)

val backgroundTypeValues = mutableListOf(
getString(commonR.string.widget_background_type_dynamiccolor),
getString(commonR.string.widget_background_type_daynight)
)
if (DynamicColors.isDynamicColorAvailable()) {
binding.backgroundType.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, backgroundTypeValues)
binding.backgroundType.setSelection(0)
binding.backgroundTypeParent.visibility = View.VISIBLE
} else {
binding.backgroundTypeParent.visibility = View.GONE
}

val backgroundTypeValues = WidgetUtils.getBackgroundOptionList(this)
binding.backgroundType.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, backgroundTypeValues)
if (mediaPlayerWidget != null) {
binding.label.setText(mediaPlayerWidget.label)
binding.widgetTextConfigEntityId.setText(mediaPlayerWidget.entityId)
binding.widgetShowVolumeButtonCheckbox.isChecked = mediaPlayerWidget.showVolume
binding.widgetShowSeekButtonsCheckbox.isChecked = mediaPlayerWidget.showSeek
binding.widgetShowSkipButtonsCheckbox.isChecked = mediaPlayerWidget.showSkip
binding.widgetShowMediaPlayerSource.isChecked = mediaPlayerWidget.showSource
binding.backgroundType.setSelection(
WidgetUtils.getSelectedBackgroundOption(
this,
mediaPlayerWidget.backgroundType,
backgroundTypeValues
)
)
val entities = runBlocking {
try {
mediaPlayerWidget.entityId.split(",").map { s ->
Expand All @@ -148,14 +145,6 @@ class MediaPlayerControlsWidgetConfigureActivity : BaseWidgetConfigureActivity()
null
}
}
binding.backgroundType.setSelection(
when {
mediaPlayerWidget.backgroundType == WidgetBackgroundType.DYNAMICCOLOR && DynamicColors.isDynamicColorAvailable() ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_dynamiccolor))
else ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_daynight))
}
)
if (entities != null) {
selectedEntities.addAll(entities)
}
Expand Down Expand Up @@ -262,6 +251,7 @@ class MediaPlayerControlsWidgetConfigureActivity : BaseWidgetConfigureActivity()
MediaPlayerControlsWidget.EXTRA_BACKGROUND_TYPE,
when (binding.backgroundType.selectedItem as String?) {
getString(commonR.string.widget_background_type_dynamiccolor) -> WidgetBackgroundType.DYNAMICCOLOR
getString(commonR.string.widget_background_type_transparent) -> WidgetBackgroundType.TRANSPARENT
else -> WidgetBackgroundType.DAYNIGHT
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.lifecycleScope
import com.fasterxml.jackson.databind.JsonMappingException
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.database.widget.TemplateWidgetDao
Expand All @@ -28,6 +27,7 @@ import io.homeassistant.companion.android.databinding.WidgetTemplateConfigureBin
import io.homeassistant.companion.android.settings.widgets.ManageWidgetsViewModel
import io.homeassistant.companion.android.util.getHexForColor
import io.homeassistant.companion.android.widgets.BaseWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.common.WidgetUtils
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -86,13 +86,7 @@ class TemplateWidgetConfigureActivity : BaseWidgetConfigureActivity() {

val templateWidget = templateWidgetDao.get(appWidgetId)

val backgroundTypeValues = mutableListOf(
getString(commonR.string.widget_background_type_daynight),
getString(commonR.string.widget_background_type_transparent)
)
if (DynamicColors.isDynamicColorAvailable()) {
backgroundTypeValues.add(0, getString(commonR.string.widget_background_type_dynamiccolor))
}
val backgroundTypeValues = WidgetUtils.getBackgroundOptionList(this)
binding.backgroundType.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, backgroundTypeValues)

setupServerSelect(templateWidget?.serverId)
Expand All @@ -107,16 +101,12 @@ class TemplateWidgetConfigureActivity : BaseWidgetConfigureActivity() {
binding.renderedTemplate.text = getString(commonR.string.empty_template)
binding.addButton.isEnabled = false
}

binding.backgroundType.setSelection(
when {
templateWidget.backgroundType == WidgetBackgroundType.DYNAMICCOLOR && DynamicColors.isDynamicColorAvailable() ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_dynamiccolor))
templateWidget.backgroundType == WidgetBackgroundType.TRANSPARENT ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_transparent))
else ->
backgroundTypeValues.indexOf(getString(commonR.string.widget_background_type_daynight))
}
WidgetUtils.getSelectedBackgroundOption(
this,
templateWidget.backgroundType,
backgroundTypeValues
)
)
binding.textColor.isVisible = templateWidget.backgroundType == WidgetBackgroundType.TRANSPARENT
binding.textColorWhite.isChecked =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone"
tools:visibility="visible">

<TextView
Expand Down

0 comments on commit b6a9934

Please sign in to comment.