|
| 1 | +package io.homeassistant.companion.android.controls |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Build |
| 5 | +import android.service.controls.Control |
| 6 | +import android.service.controls.DeviceTypes |
| 7 | +import android.service.controls.actions.BooleanAction |
| 8 | +import android.service.controls.actions.ControlAction |
| 9 | +import android.service.controls.actions.FloatAction |
| 10 | +import android.service.controls.templates.ControlButton |
| 11 | +import android.service.controls.templates.RangeTemplate |
| 12 | +import android.service.controls.templates.ToggleRangeTemplate |
| 13 | +import android.service.controls.templates.ToggleTemplate |
| 14 | +import androidx.annotation.RequiresApi |
| 15 | +import io.homeassistant.companion.android.common.R as commonR |
| 16 | +import io.homeassistant.companion.android.common.data.integration.Entity |
| 17 | +import io.homeassistant.companion.android.common.data.integration.IntegrationRepository |
| 18 | +import io.homeassistant.companion.android.common.data.integration.getVolumeLevel |
| 19 | +import io.homeassistant.companion.android.common.data.integration.getVolumeStep |
| 20 | +import io.homeassistant.companion.android.common.data.integration.isActive |
| 21 | +import io.homeassistant.companion.android.common.data.integration.supportsVolumeSet |
| 22 | +import java.math.BigDecimal |
| 23 | +import java.math.RoundingMode |
| 24 | + |
| 25 | +@RequiresApi(Build.VERSION_CODES.R) |
| 26 | +object MediaPlayerControl : HaControl { |
| 27 | + override fun provideControlFeatures( |
| 28 | + context: Context, |
| 29 | + control: Control.StatefulBuilder, |
| 30 | + entity: Entity<Map<String, Any>>, |
| 31 | + info: HaControlInfo |
| 32 | + ): Control.StatefulBuilder { |
| 33 | + if (entity.supportsVolumeSet()) { |
| 34 | + val volumeLevel = entity.getVolumeLevel() |
| 35 | + control.setControlTemplate( |
| 36 | + ToggleRangeTemplate( |
| 37 | + entity.entityId, |
| 38 | + entity.isActive(), |
| 39 | + "", |
| 40 | + RangeTemplate( |
| 41 | + entity.entityId, |
| 42 | + volumeLevel?.min ?: 0f, |
| 43 | + volumeLevel?.max ?: 100f, |
| 44 | + volumeLevel?.value ?: 0f, |
| 45 | + entity.getVolumeStep(), |
| 46 | + "%.0f%%" |
| 47 | + ) |
| 48 | + ) |
| 49 | + ) |
| 50 | + } else { |
| 51 | + control.setControlTemplate( |
| 52 | + ToggleTemplate( |
| 53 | + entity.entityId, |
| 54 | + ControlButton( |
| 55 | + entity.isActive(), |
| 56 | + "" |
| 57 | + ) |
| 58 | + ) |
| 59 | + ) |
| 60 | + } |
| 61 | + return control |
| 62 | + } |
| 63 | + |
| 64 | + override fun getDeviceType(entity: Entity<Map<String, Any>>): Int = |
| 65 | + DeviceTypes.TYPE_TV |
| 66 | + |
| 67 | + override fun getDomainString(context: Context, entity: Entity<Map<String, Any>>): String = |
| 68 | + context.getString(commonR.string.media_player) |
| 69 | + |
| 70 | + override suspend fun performAction( |
| 71 | + integrationRepository: IntegrationRepository, |
| 72 | + action: ControlAction |
| 73 | + ): Boolean { |
| 74 | + when (action) { |
| 75 | + is BooleanAction -> { |
| 76 | + integrationRepository.callAction( |
| 77 | + action.templateId.split(".")[0], |
| 78 | + "media_play_pause", |
| 79 | + hashMapOf("entity_id" to action.templateId) |
| 80 | + ) |
| 81 | + } |
| 82 | + is FloatAction -> { |
| 83 | + // Convert back to accepted format: |
| 84 | + // https://github.com/home-assistant/frontend/blob/dev/src/dialogs/more-info/controls/more-info-media_player.ts#L289 |
| 85 | + val volumeLevel = action.newValue.div(100) |
| 86 | + integrationRepository.callAction( |
| 87 | + action.templateId.split(".")[0], |
| 88 | + "volume_set", |
| 89 | + hashMapOf( |
| 90 | + "entity_id" to action.templateId, |
| 91 | + "volume_level" to BigDecimal(volumeLevel.toDouble()).setScale(2, RoundingMode.HALF_UP) |
| 92 | + ) |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + return true |
| 97 | + } |
| 98 | +} |
0 commit comments