From 464b340cbd67a6283dd27797ac1669fbe640e2af Mon Sep 17 00:00:00 2001 From: Markus Friedli Date: Sat, 1 Apr 2023 18:58:31 +0200 Subject: [PATCH 1/2] Add Android OS sensors --- .../android/sensors/SensorReceiver.kt | 2 + .../common/sensors/AndroidOsSensorManager.kt | 80 +++++++++++++++++++ common/src/main/res/values/strings.xml | 5 ++ 3 files changed, 87 insertions(+) create mode 100644 common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt index 9a2eebe1c54..417dd41e304 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt @@ -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 @@ -52,6 +53,7 @@ class SensorReceiver : SensorReceiverBase() { val MANAGERS = listOf( ActivitySensorManager(), AndroidAutoSensorManager(), + AndroidOsSensorManager(), AppSensorManager(), AudioSensorManager(), BatterySensorManager(), diff --git a/common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt b/common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt new file mode 100644 index 00000000000..b8a1c215f79 --- /dev/null +++ b/common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt @@ -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 { + return listOf(osVersion, osSecurityPatch) + } + + override fun requiredPermissions(sensorId: String): Array { + return arrayOf() + } + + override fun hasSensor(context: Context): Boolean { + return true + } + + 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)) { + 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() + ) + } +} diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index b4e0ea750bc..aa924882ff6 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -42,6 +42,8 @@ Detected Activity Next Alarm Android Auto + OS Version + Security Patch App Importance App Inactive App Memory @@ -494,6 +496,8 @@ Total count of active notifications that are visible to the user including silent, persistent and the Sensor Worker notifications. If the app is in the foreground, background or any other state it can be. If the phone is currently connected to an Android Auto head unit. + Android OS version. + Android OS security patch. Whether the app is currently considered inactive by the system Total used and available memory for the app App Rx GB since last device reboot @@ -574,6 +578,7 @@ Activity Sensors Alarm Sensor Android Auto Sensors + Android OS Sensors App Sensors Audio Mode Audio Sensors From b79b80e3caf8eb96841004375bf309cd654b578d Mon Sep 17 00:00:00 2001 From: Markus Friedli Date: Mon, 3 Apr 2023 09:03:01 +0200 Subject: [PATCH 2/2] Rework review findings --- .../android/common/sensors/AndroidOsSensorManager.kt | 12 ++++++------ .../companion/android/sensors/SensorReceiver.kt | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt b/common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt index b8a1c215f79..c1745baa8aa 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/sensors/AndroidOsSensorManager.kt @@ -33,17 +33,17 @@ class AndroidOsSensorManager : SensorManager { get() = commonR.string.sensor_name_android_os override suspend fun getAvailableSensors(context: Context): List { - return listOf(osVersion, osSecurityPatch) + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + listOf(osVersion, osSecurityPatch) + } else { + listOf(osVersion) + } } override fun requiredPermissions(sensorId: String): Array { return arrayOf() } - override fun hasSensor(context: Context): Boolean { - return true - } - override fun requestSensorUpdate( context: Context ) { @@ -55,7 +55,7 @@ class AndroidOsSensorManager : SensorManager { context: Context, sensor: SensorManager.BasicSensor ) { - if (!isEnabled(context, sensor) || !hasSensor(context)) { + if (!isEnabled(context, sensor)) { return } diff --git a/wear/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt b/wear/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt index c2b3ee8f417..0f22282656b 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt @@ -14,6 +14,7 @@ import androidx.core.app.TaskStackBuilder import androidx.core.net.toUri 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 @@ -54,6 +55,7 @@ class SensorReceiver : SensorReceiverBase() { companion object { const val TAG = "SensorReceiver" private val allManager = listOf( + AndroidOsSensorManager(), AppSensorManager(), AudioSensorManager(), BatterySensorManager(),