Colota - Android GPS Location tracker
fix(motion): move accel sensor processing off main thread (#415)
Max Dietrichd6fbd7532e31
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/RawSensorMotionDetector.kt b/apps/mobile/android/app/src/main/java/com/colota/service/RawSensorMotionDetector.ktindex 87c43508..88dc19bc 100644--- a/apps/mobile/android/app/src/main/java/com/colota/service/RawSensorMotionDetector.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/service/RawSensorMotionDetector.kt@@ -13,6 +13,7 @@ import android.hardware.SensorManagerimport android.hardware.TriggerEventimport android.hardware.TriggerEventListenerimport android.os.Handler+import android.os.HandlerThreadimport android.os.Looperimport android.os.SystemClockimport com.Colota.BuildConfig@@ -53,6 +54,9 @@ class RawSensorMotionDetector(private val mainHandler = Handler(Looper.getMainLooper())private val lifecycleLock = Any()+ private var sensorThread: HandlerThread? = null+ @Volatile private var sensorHandler: Handler? = null+@Volatile private var listener: ((MotionState) -> Unit)? = null@Volatile private var currentState: MotionState = MotionState.MOVING@@ -79,7 +83,7 @@ class RawSensorMotionDetector(private val sigMotionListener = object : TriggerEventListener() {override fun onTrigger(event: TriggerEvent?) {sigMotionSensor?.let { sensorManager.requestTriggerSensor(this, it) }- transitionTo(MotionState.MOVING, "sig_motion")+ sensorHandler?.post { transitionTo(MotionState.MOVING, "sig_motion") }}}@@ -92,8 +96,13 @@ class RawSensorMotionDetector(aboveThresholdSinceMs = 0LbelowThresholdSinceMs = 0L+ val thread = HandlerThread("MotionDetector-sensor").also { it.start() }+ val handler = Handler(thread.looper)+ sensorThread = thread+ sensorHandler = handler+accelSensor?.let {- sensorManager.registerListener(accelListener, it, SensorManager.SENSOR_DELAY_NORMAL, MAX_REPORT_LATENCY_US)+ sensorManager.registerListener(accelListener, it, SensorManager.SENSOR_DELAY_NORMAL, MAX_REPORT_LATENCY_US, handler)}sigMotionSensor?.let {sensorManager.requestTriggerSensor(sigMotionListener, it)@@ -108,17 +117,23 @@ class RawSensorMotionDetector(listener = nullaccelSensor?.let { sensorManager.unregisterListener(accelListener, it) }sigMotionSensor?.let { sensorManager.cancelTriggerSensor(sigMotionListener, it) }- sampleWindow.clear()- // Drop transition posts queued before stop arrived. The listener null-check in- // the posted lambda is the backstop if cancellation misses one.mainHandler.removeCallbacksAndMessages(null)+ sensorHandler?.removeCallbacksAndMessages(null)+ sensorThread?.quitSafely()+ sensorHandler = null+ sensorThread = null}AppLogger.d(TAG, "Stopped")}/** Test-build hook: directly inject a state transition (called from ADB-debug receiver). */fun forceState(newState: MotionState) {- transitionTo(newState, "debug_force")+ val handler = sensorHandler+ if (handler != null) {+ handler.post { transitionTo(newState, "debug_force") }+ } else {+ transitionTo(newState, "debug_force")+ }}private fun processSample(nowMs: Long, magnitude: Double) {diff --git a/apps/mobile/android/app/src/test/java/com/Colota/service/RawSensorMotionDetectorTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/service/RawSensorMotionDetectorTest.ktindex f9504a23..6a84b86d 100644--- a/apps/mobile/android/app/src/test/java/com/Colota/service/RawSensorMotionDetectorTest.kt+++ b/apps/mobile/android/app/src/test/java/com/Colota/service/RawSensorMotionDetectorTest.kt@@ -13,6 +13,7 @@ import android.hardware.SensorManagerimport android.hardware.TriggerEventimport android.hardware.TriggerEventListenerimport android.os.Handler+import android.os.HandlerThreadimport android.os.Looperimport android.os.SystemClockimport com.Colota.util.AppLogger@@ -53,6 +54,10 @@ class RawSensorMotionDetectorTest {firstArg<Runnable>().run()true}+ mockkConstructor(HandlerThread::class)+ every { anyConstructed<HandlerThread>().start() } just Runs+ every { anyConstructed<HandlerThread>().looper } returns mockk(relaxed = true)+ every { anyConstructed<HandlerThread>().quitSafely() } returns true// SystemClock.elapsedRealtime is read inside accelListener.onSensorChanged; tests// that drive synthetic sensor events stub it via stubElapsedRealtime() per sample.@@ -68,6 +73,7 @@ class RawSensorMotionDetectorTest {unmockkStatic(Looper::class)unmockkStatic(SystemClock::class)unmockkConstructor(Handler::class)+ unmockkConstructor(HandlerThread::class)}private fun makeDetector(@@ -80,7 +86,7 @@ class RawSensorMotionDetectorTest {every { sensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION) } returnsif (sigMotionAvailable) sigMotionSensor else nullevery { sensorManager.requestTriggerSensor(capture(sigListenerSlot), any()) } returns true- every { sensorManager.registerListener(capture(accelListenerSlot), any<Sensor>(), any<Int>(), any<Int>()) } returns true+ every { sensorManager.registerListener(capture(accelListenerSlot), any<Sensor>(), any<Int>(), any<Int>(), any<Handler>()) } returns truereturn RawSensorMotionDetector(context) { stationaryDwellMs }}@@ -124,7 +130,7 @@ class RawSensorMotionDetectorTest {fun `start registers accelerometer with batched FIFO and SIG_MOTION`() {val detector = makeDetector()detector.start { /* no-op */ }- verify { sensorManager.registerListener(any(), accelSensor, SensorManager.SENSOR_DELAY_NORMAL, 30_000_000) }+ verify { sensorManager.registerListener(any(), accelSensor, SensorManager.SENSOR_DELAY_NORMAL, 30_000_000, any<Handler>()) }verify { sensorManager.requestTriggerSensor(any(), sigMotionSensor) }}@@ -133,7 +139,7 @@ class RawSensorMotionDetectorTest {val detector = makeDetector()detector.start { }detector.start { }- verify(exactly = 1) { sensorManager.registerListener(any(), accelSensor, any(), any<Int>()) }+ verify(exactly = 1) { sensorManager.registerListener(any(), accelSensor, any(), any<Int>(), any<Handler>()) }verify(exactly = 1) { sensorManager.requestTriggerSensor(any(), sigMotionSensor) }}@@ -158,7 +164,7 @@ class RawSensorMotionDetectorTest {fun `start works when SIG_MOTION sensor is missing`() {val detector = makeDetector(sigMotionAvailable = false)detector.start { }- verify { sensorManager.registerListener(any(), accelSensor, any(), any<Int>()) }+ verify { sensorManager.registerListener(any(), accelSensor, any(), any<Int>(), any<Handler>()) }verify(exactly = 0) { sensorManager.requestTriggerSensor(any(), any()) }}@@ -201,9 +207,10 @@ class RawSensorMotionDetectorTest {detector.stop()- // Drain the post that was queued before stop() ran. The defensive null-check- // in the posted lambda should make it a no-op now that listener is null.- pending.forEach { it.run() }+ // Drain loop (not forEach): pending grows mid-iteration as runnables enqueue more.+ while (pending.isNotEmpty()) {+ pending.removeAt(0).run()+ }assertNull(received)}