mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Add do not disturb sensor (#813)
* Initial commit of DND sensor * Update sensor name * Adapt to new sensor model * Review comments
This commit is contained in:
parent
a958b0f95c
commit
9cae26b771
|
@ -1,6 +1,7 @@
|
|||
package io.homeassistant.companion.android
|
||||
|
||||
import android.app.Application
|
||||
import android.app.NotificationManager
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
|
@ -77,6 +78,14 @@ open class HomeAssistantApplication : Application(), GraphComponentAccessor {
|
|||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Add receiver for DND changes on devices that support it
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
registerReceiver(
|
||||
sensorReceiver,
|
||||
IntentFilter(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override val appComponent: AppComponent
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package io.homeassistant.companion.android.sensors
|
||||
|
||||
import android.content.Context
|
||||
import android.provider.Settings.Global
|
||||
import android.util.Log
|
||||
|
||||
class DNDSensorManager : SensorManager {
|
||||
companion object {
|
||||
private const val TAG = "DNDSensor"
|
||||
|
||||
private val dndSensor = SensorManager.BasicSensor(
|
||||
"dnd_sensor",
|
||||
"sensor",
|
||||
"Do Not Disturb Sensor"
|
||||
)
|
||||
}
|
||||
|
||||
override val name: String
|
||||
get() = "Do Not Disturb Sensor"
|
||||
|
||||
override val availableSensors: List<SensorManager.BasicSensor>
|
||||
get() = listOf(dndSensor)
|
||||
|
||||
override fun requiredPermissions(): Array<String> {
|
||||
return emptyArray()
|
||||
}
|
||||
|
||||
override fun requestSensorUpdate(context: Context) {
|
||||
updateDNDState(context)
|
||||
}
|
||||
|
||||
private fun updateDNDState(context: Context) {
|
||||
|
||||
if (!isEnabled(context, dndSensor.id))
|
||||
return
|
||||
|
||||
var dndState = "unavailable"
|
||||
try {
|
||||
dndState = when (Global.getInt(context.contentResolver, "zen_mode")) {
|
||||
0 -> "off"
|
||||
1 -> "priority_only"
|
||||
2 -> "total_silence"
|
||||
3 -> "alarms_only"
|
||||
else -> "unknown"
|
||||
}
|
||||
val icon = "mdi:do-not-disturb"
|
||||
|
||||
onSensorUpdated(context,
|
||||
dndSensor,
|
||||
dndState,
|
||||
icon,
|
||||
mapOf()
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error getting the devices DND mode", e)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ class SensorReceiver : BroadcastReceiver() {
|
|||
AudioSensorManager(),
|
||||
BatterySensorManager(),
|
||||
BluetoothSensorManager(),
|
||||
DNDSensorManager(),
|
||||
GeocodeSensorManager(),
|
||||
LastRebootSensorManager(),
|
||||
LightSensorManager(),
|
||||
|
|
Loading…
Reference in a new issue