mirror of
https://github.com/home-assistant/android
synced 2024-07-22 10:54:12 +00:00
Show custom icon if set for the entity in device controls (#2922)
* Add support for showing custom icons in device controls * Clean up and set color based on state * Set icon size and adjust colors based on domain * Move color values to resources and add more color options from source * Review comments * Only set the icon if it exists in the library
This commit is contained in:
parent
ceac76cd9c
commit
60aadc906a
|
@ -1,5 +1,6 @@
|
|||
package io.homeassistant.companion.android.controls
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
|
@ -7,15 +8,21 @@ import android.os.Build
|
|||
import android.service.controls.Control
|
||||
import android.service.controls.actions.ControlAction
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.mikepenz.iconics.IconicsDrawable
|
||||
import com.mikepenz.iconics.utils.sizeDp
|
||||
import com.mikepenz.iconics.utils.toAndroidIconCompat
|
||||
import io.homeassistant.companion.android.common.R
|
||||
import io.homeassistant.companion.android.common.data.integration.Entity
|
||||
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
|
||||
import io.homeassistant.companion.android.common.data.integration.domain
|
||||
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
|
||||
import io.homeassistant.companion.android.webview.WebViewActivity
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.R)
|
||||
interface HaControl {
|
||||
|
||||
@SuppressLint("ResourceType")
|
||||
fun createControl(
|
||||
context: Context,
|
||||
entity: Entity<Map<String, Any>>,
|
||||
|
@ -49,6 +56,31 @@ interface HaControl {
|
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
control.setAuthRequired(authRequired)
|
||||
}
|
||||
if (entity.attributes["icon"]?.toString()?.startsWith("mdi:") == true &&
|
||||
!entity.attributes["icon"]?.toString()?.substringAfter(":").isNullOrBlank()
|
||||
) {
|
||||
val iconName = entity.attributes["icon"]!!.toString().split(':')[1]
|
||||
val iconDrawable =
|
||||
IconicsDrawable(context, "cmd-$iconName").apply {
|
||||
sizeDp = 48
|
||||
}
|
||||
if (iconDrawable.icon != null) {
|
||||
val colorTint = when {
|
||||
entity.domain == "light" && entity.state == "on" -> R.color.colorDeviceControlsLightOn
|
||||
entity.domain == "camera" -> R.color.colorDeviceControlsCamera
|
||||
entity.domain == "climate" && entity.state == "heat" -> R.color.colorDeviceControlsThermostatHeat
|
||||
entity.state in listOf(
|
||||
"off",
|
||||
"unavailable",
|
||||
"unknown"
|
||||
) -> R.color.colorDeviceControlsOff
|
||||
else -> R.color.colorDeviceControlsDefaultOn
|
||||
}
|
||||
|
||||
iconDrawable.setTint(ContextCompat.getColor(context, colorTint))
|
||||
control.setCustomIcon(iconDrawable.toAndroidIconCompat().toIcon(context))
|
||||
}
|
||||
}
|
||||
|
||||
return provideControlFeatures(context, control, entity, area, baseUrl).build()
|
||||
}
|
||||
|
|
|
@ -27,4 +27,9 @@
|
|||
<color name="colorCodeBackground">#f5f5f5</color>
|
||||
<color name="colorAlertWarning">#1fffa600</color>
|
||||
<color name="colorOnAlertWarning">#ffa600</color>
|
||||
<color name="colorDeviceControlsLightOn">#FDD663</color>
|
||||
<color name="colorDeviceControlsOff">#9AA0A6</color>
|
||||
<color name="colorDeviceControlsDefaultOn">#8AB4F8</color>
|
||||
<color name="colorDeviceControlsThermostatHeat">#FF8B66</color>
|
||||
<color name="colorDeviceControlsCamera">#F1F3F4</color>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue