Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2 different display sensors around various screen states #4634

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ open class HomeAssistantApplication : Application() {
)
}

// Register for changes to the configuration
ContextCompat.registerReceiver(
this,
sensorReceiver,
IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED),
ContextCompat.RECEIVER_EXPORTED
)

// Update widgets when the screen turns on, updates are skipped if widgets were not added
val buttonWidget = ButtonWidget()
val entityWidget = EntityWidget()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ class SensorReceiver : SensorReceiverBase() {
Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE to listOf(DevicePolicyManager.isWorkProfile.id),
Intent.ACTION_MANAGED_PROFILE_AVAILABLE to listOf(DevicePolicyManager.isWorkProfile.id),
WifiManager.WIFI_STATE_CHANGED_ACTION to listOf(NetworkSensorManager.wifiState.id),
NfcAdapter.ACTION_ADAPTER_STATE_CHANGED to listOf(NfcSensorManager.nfcStateSensor.id)
NfcAdapter.ACTION_ADAPTER_STATE_CHANGED to listOf(NfcSensorManager.nfcStateSensor.id),
Intent.ACTION_CONFIGURATION_CHANGED to listOf(
DisplaySensorManager.screenOrientation.id,
DisplaySensorManager.screenRotation.id
)
)

override fun getSensorSettingsIntent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package io.homeassistant.companion.android.common.sensors

import android.content.Context
import android.content.res.Configuration
import android.hardware.display.DisplayManager
import android.provider.Settings
import android.util.Log
import android.view.Display
import android.view.Surface
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN

class DisplaySensorManager : SensorManager {
companion object {
private const val TAG = "ScreenBrightness"
private const val TAG = "DisplaySensors"

val screenBrightness = SensorManager.BasicSensor(
"screen_brightness",
Expand All @@ -26,23 +31,50 @@ class DisplaySensorManager : SensorManager {
"mdi:cellphone-off",
docsLink = "https://companion.home-assistant.io/docs/core/sensors#screen-off-timeout-sensor"
)

val screenOrientation = SensorManager.BasicSensor(
"screen_orientation",
"sensor",
commonR.string.sensor_name_screen_orientation,
commonR.string.sensor_description_screen_orientation,
"mdi:screen-rotation",
docsLink = "https://companion.home-assistant.io/docs/core/sensors#screen-orientation-sensor",
updateType = SensorManager.BasicSensor.UpdateType.INTENT
)

val screenRotation = SensorManager.BasicSensor(
"screen_rotation",
"sensor",
commonR.string.sensor_name_screen_rotation,
commonR.string.sensor_description_screen_rotation,
"mdi:screen-rotation",
docsLink = "https://companion.home-assistant.io/docs/core/sensors#screen-rotation-sensor",
unitOfMeasurement = "°"
)
}

override val name: Int
get() = commonR.string.sensor_name_display_sensors

override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
return listOf(screenBrightness, screenOffTimeout)
return listOf(screenBrightness, screenOffTimeout, screenOrientation, screenRotation)
}

override fun requiredPermissions(sensorId: String): Array<String> {
return emptyArray()
}

override fun docsLink(): String {
return "https://companion.home-assistant.io/docs/core/sensors#display-sensors"
}

override fun requestSensorUpdate(
context: Context
) {
updateScreenBrightness(context)
updateScreenTimeout(context)
updateScreenOrientation(context)
updateScreenRotation(context)
}

private fun updateScreenBrightness(context: Context) {
Expand Down Expand Up @@ -98,4 +130,69 @@ class DisplaySensorManager : SensorManager {
mapOf()
)
}

private fun updateScreenOrientation(context: Context) {
if (!isEnabled(context, screenOrientation)) {
return
}

@Suppress("DEPRECATION")
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved
val orientation = when (context.resources.configuration.orientation) {
Configuration.ORIENTATION_PORTRAIT -> "portrait"
Configuration.ORIENTATION_LANDSCAPE -> "landscape"
Configuration.ORIENTATION_SQUARE -> "square"
Configuration.ORIENTATION_UNDEFINED -> STATE_UNKNOWN
else -> STATE_UNKNOWN
}
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved

val icon = when (orientation) {
"portrait" -> "mdi:phone-rotate-portrait"
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved
"landscape" -> "mdi:phone-rotate-landscape"
"square" -> "mdi:crop-square"
else -> screenOrientation.statelessIcon
}

onSensorUpdated(
context,
screenOrientation,
orientation,
icon,
mapOf(
"options" to listOf("portrait", "landscape", "square", STATE_UNKNOWN)
)
)
}

private fun updateScreenRotation(context: Context) {
if (!isEnabled(context, screenRotation)) {
return
}

val dm = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager

val display = getRotationString(dm.getDisplay(Display.DEFAULT_DISPLAY).rotation)

val multiple = dm.displays.associate { it.name to getRotationString(it.rotation) }
val possibleStates = listOf("0", "90", "180", "270")
val attrs = if (dm.displays.size > 1) {
multiple.plus("options" to possibleStates)
} else {
mapOf("options" to possibleStates)
}
onSensorUpdated(
context,
screenRotation,
display,
screenRotation.statelessIcon,
attrs
)
}

private fun getRotationString(rotate: Int): String = when (rotate) {
Surface.ROTATION_0 -> "0"
Surface.ROTATION_90 -> "90"
Surface.ROTATION_180 -> "180"
Surface.ROTATION_270 -> "270"
else -> STATE_UNKNOWN
}
}
4 changes: 4 additions & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1283,4 +1283,8 @@
<string name="basic_sensor_name_sim_1_data_network_type">Data network type (SIM 1)</string>
<string name="basic_sensor_name_sim_2_data_network_type">Data network type (SIM 2)</string>
<string name="sensor_description_data_network_type">The radio technology (network type) currently in use on the device for data transmission</string>
<string name="sensor_name_screen_orientation">Screen orientation</string>
<string name="sensor_description_screen_orientation">Overall orientation of the screen</string>
<string name="sensor_name_screen_rotation">Screen rotation</string>
<string name="sensor_description_screen_rotation">The rotation of the screen from its \"natural\" orientation</string>
</resources>