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 Android OS sensors #3449

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.net.wifi.WifiManager
import android.os.PowerManager
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.sensors.AndroidOsSensorManager
import io.homeassistant.companion.android.common.sensors.AudioSensorManager
import io.homeassistant.companion.android.common.sensors.BatterySensorManager
import io.homeassistant.companion.android.common.sensors.BluetoothSensorManager
Expand Down Expand Up @@ -52,6 +53,7 @@ class SensorReceiver : SensorReceiverBase() {
val MANAGERS = listOf(
ActivitySensorManager(),
AndroidAutoSensorManager(),
AndroidOsSensorManager(),
AppSensorManager(),
AudioSensorManager(),
BatterySensorManager(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.homeassistant.companion.android.common.sensors

import android.content.Context
import android.os.Build
import io.homeassistant.companion.android.common.R as commonR

class AndroidOsSensorManager : SensorManager {

companion object {
val osVersion = SensorManager.BasicSensor(
"android_os_version",
"sensor",
commonR.string.basic_sensor_name_android_os_version,
commonR.string.sensor_description_android_os_version,
"mdi:android",
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC
)
val osSecurityPatch = SensorManager.BasicSensor(
"android_os_security_patch",
"sensor",
commonR.string.basic_sensor_name_android_os_security_patch,
commonR.string.sensor_description_android_os_security_patch,
"mdi:security",
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC
)
}

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

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

override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
return listOf(osVersion, osSecurityPatch)
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
}

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

override fun hasSensor(context: Context): Boolean {
return true
}
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved

override fun requestSensorUpdate(
context: Context
) {
checkState(context, osVersion)
checkState(context, osSecurityPatch)
}

private fun checkState(
context: Context,
sensor: SensorManager.BasicSensor
) {
if (!isEnabled(context, sensor) || !hasSensor(context)) {
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
return
}

onSensorUpdated(
context,
sensor,
when (sensor.id) {
osVersion.id -> Build.VERSION.RELEASE
osSecurityPatch.id -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Build.VERSION.SECURITY_PATCH
} else {
"unknown"
}
else -> {
"unknown"
}
},
sensor.statelessIcon,
mapOf()
)
}
}
5 changes: 5 additions & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<string name="basic_sensor_name_activity">Detected Activity</string>
<string name="basic_sensor_name_alarm">Next Alarm</string>
<string name="basic_sensor_name_android_auto">Android Auto</string>
<string name="basic_sensor_name_android_os_version">OS Version</string>
<string name="basic_sensor_name_android_os_security_patch">Security Patch</string>
<string name="basic_sensor_name_app_importance">App Importance</string>
<string name="basic_sensor_name_app_inactive">App Inactive</string>
<string name="basic_sensor_name_app_memory">App Memory</string>
Expand Down Expand Up @@ -494,6 +496,8 @@
<string name="sensor_description_active_notification_count">Total count of active notifications that are visible to the user including silent, persistent and the Sensor Worker notifications.</string>
<string name="sensor_description_app_importance">If the app is in the foreground, background or any other state it can be.</string>
<string name="sensor_description_android_auto">If the phone is currently connected to an Android Auto head unit.</string>
<string name="sensor_description_android_os_version">Android OS version.</string>
<string name="sensor_description_android_os_security_patch">Android OS security patch.</string>
<string name="sensor_description_app_inactive">Whether the app is currently considered inactive by the system</string>
<string name="sensor_description_app_memory">Total used and available memory for the app</string>
<string name="sensor_description_app_rx_gb">App Rx GB since last device reboot</string>
Expand Down Expand Up @@ -574,6 +578,7 @@
<string name="sensor_name_activity">Activity Sensors</string>
<string name="sensor_name_alarm">Alarm Sensor</string>
<string name="sensor_name_android_auto">Android Auto Sensors</string>
<string name="sensor_name_android_os">Android OS Sensors</string>
<string name="sensor_name_app_sensor">App Sensors</string>
<string name="sensor_name_audio_mode">Audio Mode</string>
<string name="sensor_name_audio">Audio Sensors</string>
Expand Down