mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Add steps counter sensor (#792)
* Add steps sensor * Fix permission and set correct icon
This commit is contained in:
parent
8e5f063200
commit
c61bf25854
|
@ -15,6 +15,7 @@
|
|||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
<uses-permission android:name="android.permission.NFC" />
|
||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
|
||||
|
||||
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
|
||||
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
|
||||
|
|
|
@ -27,6 +27,7 @@ class SensorReceiver : BroadcastReceiver() {
|
|||
LightSensorManager(),
|
||||
NextAlarmManager(),
|
||||
PhoneStateSensorManager(),
|
||||
StepsSensorManager(),
|
||||
StorageSensorManager()
|
||||
)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
package io.homeassistant.companion.android.sensors
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.Context.SENSOR_SERVICE
|
||||
import android.hardware.Sensor
|
||||
import android.hardware.SensorEvent
|
||||
import android.hardware.SensorEventListener
|
||||
import android.hardware.SensorManager.SENSOR_DELAY_NORMAL
|
||||
import android.os.Build
|
||||
import io.homeassistant.companion.android.domain.integration.SensorRegistration
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class StepsSensorManager : SensorManager, SensorEventListener {
|
||||
companion object {
|
||||
|
||||
private const val TAG = "StepsSensor"
|
||||
private val stepsSensor = SensorManager.BasicSensor(
|
||||
"steps_sensor",
|
||||
"sensor",
|
||||
"Steps Sensor",
|
||||
unitOfMeasurement = "steps"
|
||||
)
|
||||
private var stepsReading: String = "unavailable"
|
||||
lateinit var mySensorManager: android.hardware.SensorManager
|
||||
}
|
||||
|
||||
override val name: String
|
||||
get() = "Steps Sensors"
|
||||
|
||||
override val availableSensors: List<SensorManager.BasicSensor>
|
||||
get() = listOf(stepsSensor)
|
||||
|
||||
override fun requiredPermissions(): Array<String> {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
arrayOf(
|
||||
Manifest.permission.ACTIVITY_RECOGNITION
|
||||
)
|
||||
} else {
|
||||
arrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getSensorData(
|
||||
context: Context,
|
||||
sensorId: String
|
||||
): SensorRegistration<Any> {
|
||||
return when (sensorId) {
|
||||
stepsSensor.id -> getStepsSensor(context)
|
||||
else -> throw IllegalArgumentException("Unknown sensorId: $sensorId")
|
||||
}
|
||||
}
|
||||
|
||||
private fun getStepsSensor(context: Context): SensorRegistration<Any> {
|
||||
|
||||
if (checkPermission(context)) {
|
||||
mySensorManager =
|
||||
context.getSystemService(SENSOR_SERVICE) as android.hardware.SensorManager
|
||||
|
||||
val stepsSensors = mySensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
|
||||
if (stepsSensors != null) {
|
||||
mySensorManager.registerListener(
|
||||
this,
|
||||
stepsSensors,
|
||||
SENSOR_DELAY_NORMAL
|
||||
)
|
||||
}
|
||||
}
|
||||
val icon = "mdi:walk"
|
||||
|
||||
return stepsSensor.toSensorRegistration(
|
||||
stepsReading,
|
||||
icon,
|
||||
mapOf()
|
||||
)
|
||||
}
|
||||
|
||||
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
|
||||
// Nothing happening here but we are required to call onAccuracyChanged for sensor events
|
||||
}
|
||||
|
||||
override fun onSensorChanged(event: SensorEvent?) {
|
||||
if (event != null) {
|
||||
if (event.sensor.type == Sensor.TYPE_STEP_COUNTER) {
|
||||
stepsReading = event.values[0].roundToInt().toString()
|
||||
}
|
||||
}
|
||||
mySensorManager.unregisterListener(this)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue