Skip to content

Commit

Permalink
Change face down logic to include different states
Browse files Browse the repository at this point in the history
  • Loading branch information
dshokouhi committed Sep 19, 2024
1 parent 558f396 commit c284060
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ class DisplaySensorManager : SensorManager, SensorEventListener {
unitOfMeasurement = "°"
)

val isFaceDown = SensorManager.BasicSensor(
"is_face_down",
"binary_sensor",
commonR.string.sensor_name_is_face_down,
commonR.string.sensor_description_is_face_down,
val isFaceDownOrUp = SensorManager.BasicSensor(
"is_face_down_or_up",
"sensor",
commonR.string.sensor_name_is_face_down_or_up,
commonR.string.sensor_description_is_face_down_or_up,
"mdi:hand-pointing-down",
docsLink = "https://companion.home-assistant.io/docs/core/sensors#is-face-down-sensor"
docsLink = "https://companion.home-assistant.io/docs/core/sensors#is-face-down-or-up-sensor"
)
}
override val name: Int
Expand All @@ -76,7 +76,7 @@ class DisplaySensorManager : SensorManager, SensorEventListener {
override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
hasAccelerometer = context.packageManager.hasSystemFeature(FEATURE_SENSOR_ACCELEROMETER)
return if (hasAccelerometer) {
listOf(screenBrightness, screenOffTimeout, screenOrientation, screenRotation, isFaceDown)
listOf(screenBrightness, screenOffTimeout, screenOrientation, screenRotation, isFaceDownOrUp)
} else {
listOf(screenBrightness, screenOffTimeout, screenOrientation, screenRotation)
}
Expand All @@ -102,7 +102,7 @@ class DisplaySensorManager : SensorManager, SensorEventListener {
updateScreenOrientation(context)
updateScreenRotation(context)
if (hasAccelerometer) {
updateIsFaceDown(context)
updateIsFaceDownOrUp(context)
}
}

Expand Down Expand Up @@ -217,8 +217,8 @@ class DisplaySensorManager : SensorManager, SensorEventListener {
)
}

private fun updateIsFaceDown(context: Context) {
if (!isEnabled(context, isFaceDown)) {
private fun updateIsFaceDownOrUp(context: Context) {
if (!isEnabled(context, isFaceDownOrUp)) {
return
}
val now = System.currentTimeMillis()
Expand Down Expand Up @@ -246,12 +246,26 @@ class DisplaySensorManager : SensorManager, SensorEventListener {
if (event?.sensor?.type == Sensor.TYPE_ACCELEROMETER) {
// Following the example from: https://developer.android.com/reference/android/hardware/SensorEvent#values
// When the device is lying flat with the screen down the value is inverted
val state = when {
event.values[2] < -9 -> "down"
event.values[2] > 9 -> "up"
else -> STATE_UNKNOWN
}
val icon = when (state) {
"down" -> "mdi:hand-pointing-down"
"up" -> "mdi:hand-pointing-up"
else -> "mdi:crosshairs-question"
}
onSensorUpdated(
latestContext,
isFaceDown,
event.values[2] < -9,
isFaceDown.statelessIcon,
mapOf()
isFaceDownOrUp,
state,
icon,
mapOf(
"x" to event.values[0],
"y" to event.values[1],
"z" to event.values[2]
)
)
}

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,6 @@
<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. If the device has multiple displays the state will be for the default display. Attributes will exist for additional detected displays with the display name and its rotation angle.</string>
<string name="sensor_name_is_face_down">Is face down</string>
<string name="sensor_description_is_face_down">If the device screen is facing down at the ground</string>
<string name="sensor_name_is_face_down_or_up">Is face down or up</string>
<string name="sensor_description_is_face_down_or_up">If the device screen is facing down at the ground, up at the air or unknown</string>
</resources>

0 comments on commit c284060

Please sign in to comment.