-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSensorLogic.kt
More file actions
48 lines (41 loc) · 1.77 KB
/
SensorLogic.kt
File metadata and controls
48 lines (41 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class AccelerometerRepository @Inject constructor(
private val sensorManager: SensorManager
) {
// Threshold: -8.0 allows for slight tilt (not perfectly flat), but definitely "down"
private val faceDownThreshold = -8.0f
/**
* Converts the legacy SensorManager callback system into a cold Flow.
* This ensures listeners are only active when the UI is actually observing the data,
* saving significant battery life.
*/
val orientationFlow: Flow<OrientationState> = callbackFlow {
val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
val listener = object : SensorEventListener {
override fun onSensorChanged(event: SensorEvent?) {
event?.let {
val zAxis = it.values[2] // Index 2 is the Z-axis
// Map raw physics to Domain State
val newState = if (zAxis < faceDownThreshold) {
OrientationState.FACE_DOWN
} else {
OrientationState.FACE_UP
}
trySend(newState)
}
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
// No-op for this use case
}
}
// Register the listener (Sampling rate: UI is sufficient for now)
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI)
// Cleanup when the Flow collection stops (Crucial for battery!)
awaitClose {
sensorManager.unregisterListener(listener)
}
}
// Optimization: Only emit if the state actually changes
.distinctUntilChanged()
// Optimization: Run on IO thread
.flowOn(Dispatchers.IO)
}