-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from mutualmobile/sm/low_latency_off_body_dete…
…ct_sensor Support for Low Latency Off-Body Detect sensor
- Loading branch information
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...nsors/src/main/java/com/mutualmobile/composesensors/LowLatencyOffBodyDetectSensorState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.mutualmobile.composesensors | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
|
||
/** | ||
* A low latency off body detect sensor that returns an event every time the device transitions from off-body to on-body and from on-body to off-body (e.g. a wearable device being removed from the wrist would trigger an event indicating an off-body transition). | ||
* This sensor deliver the initial on-body or off-body event representing the current device state within 5 seconds of activating the sensor. | ||
* This sensor must be able to detect and report an on-body to off-body transition within 1 second of the device being removed from the body, and must be able to detect and report an off-body to on-body transition within 5 seconds of the device being put back onto the body. | ||
* @param isDeviceOnBody This sensor produces only two values true and false where true means currently device is in on-body state and false means device is in off-body state and Defaults set to false. | ||
* @param isAvailable Whether the current device has an low latency off body detect sensor. Defaults to false. | ||
* @param accuracy Accuracy factor of the low latency off body detect sensor. Defaults to 0. | ||
*/ | ||
@Immutable | ||
class LowLatencyOffBodyDetectSensorState internal constructor( | ||
val isDeviceOnBody: Boolean = false, | ||
val isAvailable: Boolean = false, | ||
val accuracy: Int = 0 | ||
) { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is LowLatencyOffBodyDetectSensorState) return false | ||
|
||
if (isDeviceOnBody != other.isDeviceOnBody) return false | ||
if (isAvailable != other.isAvailable) return false | ||
if (accuracy != other.accuracy) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = isDeviceOnBody.hashCode() | ||
result = 31 * result + isAvailable.hashCode() | ||
result = 31 * result + accuracy.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String { | ||
return "LowLatencyOffBodyDetectSensorState(isDeviceOnBody=$isDeviceOnBody, isAvailable=$isAvailable, accuracy=$accuracy)" | ||
} | ||
} | ||
|
||
/** | ||
* Creates and [remember]s an instance of [LowLatencyOffBodyDetectSensorState]. | ||
* @param sensorDelay The rate at which the raw sensor data should be received. | ||
* Defaults to [SensorDelay.Normal]. | ||
* @param onError Callback invoked on every error state. | ||
*/ | ||
@Composable | ||
fun rememberLowLatencyOffBodyDetectSensorState( | ||
sensorDelay: SensorDelay = SensorDelay.Normal, | ||
onError: (throwable: Throwable) -> Unit = {} | ||
): LowLatencyOffBodyDetectSensorState { | ||
val sensorState = rememberSensorState( | ||
sensorType = SensorType.LowLatencyOffBodyDetect, | ||
sensorDelay = sensorDelay, | ||
onError = onError | ||
) | ||
val lowLatencyOffBodyDetectSensorState = | ||
remember { mutableStateOf(LowLatencyOffBodyDetectSensorState()) } | ||
|
||
LaunchedEffect( | ||
key1 = sensorState, | ||
block = { | ||
val sensorStateValues = sensorState.data | ||
if (sensorStateValues.isNotEmpty()) { | ||
lowLatencyOffBodyDetectSensorState.value = LowLatencyOffBodyDetectSensorState( | ||
isDeviceOnBody = sensorStateValues[0].toInt() == 1, | ||
isAvailable = sensorState.isAvailable, | ||
accuracy = sensorState.accuracy | ||
) | ||
} | ||
} | ||
) | ||
|
||
return lowLatencyOffBodyDetectSensorState.value | ||
} |