diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/AudioSensorManager.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/AudioSensorManager.kt index 8f9b0bb46a0..8177bfba5ec 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/AudioSensorManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/AudioSensorManager.kt @@ -10,7 +10,7 @@ class AudioSensorManager : SensorManager { companion object { private const val TAG = "AudioSensor" - private val audioSensor = SensorManager.BasicSensor( + val audioSensor = SensorManager.BasicSensor( "audio_sensor", "sensor", R.string.sensor_name_audio, diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/BatterySensorManager.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/BatterySensorManager.kt index ab0265e20c6..27a732fad7c 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/BatterySensorManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/BatterySensorManager.kt @@ -10,7 +10,7 @@ class BatterySensorManager : SensorManager { companion object { private const val TAG = "BatterySensor" - private val batteryLevel = SensorManager.BasicSensor( + val batteryLevel = SensorManager.BasicSensor( "battery_level", "sensor", R.string.basic_sensor_name_battery_level, diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/DNDSensorManager.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/DNDSensorManager.kt index 0d2ff81bca8..a6f10e15870 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/DNDSensorManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/DNDSensorManager.kt @@ -9,7 +9,7 @@ class DNDSensorManager : SensorManager { companion object { private const val TAG = "DNDSensor" - private val dndSensor = SensorManager.BasicSensor( + val dndSensor = SensorManager.BasicSensor( "dnd_sensor", "sensor", R.string.sensor_name_dnd, diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/PowerSensorManager.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/PowerSensorManager.kt index 904232c30e7..ec28c3bd26d 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/PowerSensorManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/PowerSensorManager.kt @@ -12,19 +12,19 @@ class PowerSensorManager : SensorManager { private const val TAG = "PowerSensors" private const val packageName = "io.homeassistant.companion.android" - private val interactiveDevice = SensorManager.BasicSensor( + val interactiveDevice = SensorManager.BasicSensor( "is_interactive", "binary_sensor", R.string.basic_sensor_name_interactive, R.string.sensor_description_interactive ) - private val doze = SensorManager.BasicSensor( + val doze = SensorManager.BasicSensor( "is_idle", "binary_sensor", R.string.basic_sensor_name_doze, R.string.sensor_description_doze ) - private val powerSave = SensorManager.BasicSensor( + val powerSave = SensorManager.BasicSensor( "power_save", "binary_sensor", R.string.basic_sensor_name_power_save, 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 f40fb37614a..6807544c179 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 @@ -1,9 +1,14 @@ package io.homeassistant.companion.android.sensors +import android.app.NotificationManager import android.bluetooth.BluetoothAdapter import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.media.AudioManager +import android.net.wifi.WifiManager +import android.os.PowerManager +import android.telephony.TelephonyManager import android.util.Log import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor import io.homeassistant.companion.android.database.AppDatabase @@ -81,6 +86,78 @@ class SensorReceiver : BroadcastReceiver() { return } } + Intent.ACTION_BATTERY_LOW, + Intent.ACTION_BATTERY_OKAY, + Intent.ACTION_POWER_CONNECTED, + Intent.ACTION_POWER_DISCONNECTED -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(BatterySensorManager.batteryLevel.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Battery Sensor disabled, skipping sensors update") + return + } + } + WifiManager.NETWORK_STATE_CHANGED_ACTION -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(NetworkSensorManager.wifiConnection.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Wifi Connection Sensor disabled, skipping sensors update") + return + } + } + Intent.ACTION_SCREEN_OFF, + Intent.ACTION_SCREEN_ON -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(PowerSensorManager.interactiveDevice.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Interactive Sensor disabled, skipping sensors update") + return + } + } + PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(PowerSensorManager.doze.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Doze Mode Sensor disabled, skipping sensors update") + return + } + } + PowerManager.ACTION_POWER_SAVE_MODE_CHANGED -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(PowerSensorManager.powerSave.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Power Save Sensor disabled, skipping sensors update") + return + } + } + TelephonyManager.ACTION_PHONE_STATE_CHANGED -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(PhoneStateSensorManager.phoneState.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Phone State Sensor disabled, skipping sensors update") + return + } + } + AudioManager.ACTION_AUDIO_BECOMING_NOISY, + AudioManager.ACTION_HEADSET_PLUG, + AudioManager.RINGER_MODE_CHANGED_ACTION, + AudioManager.ACTION_MICROPHONE_MUTE_CHANGED, + AudioManager.ACTION_SPEAKERPHONE_STATE_CHANGED -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(AudioSensorManager.audioSensor.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Audio Sensor disabled, skipping sensors update") + return + } + } + NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED -> { + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val sensor = sensorDao.get(DNDSensorManager.dndSensor.id) + if (sensor?.enabled != true) { + Log.d(TAG, "Do Not Disturb Sensor disabled, skipping sensors update") + return + } + } } ioScope.launch {