Skip to content

Commit 033b1f7

Browse files
committed
move init to start
1 parent d21a04a commit 033b1f7

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

location/src/main/kotlin/io/customer/location/LocationLifecycleObserver.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@ import androidx.lifecycle.DefaultLifecycleObserver
44
import androidx.lifecycle.LifecycleOwner
55

66
/**
7-
* Cancels in-flight location requests when the app enters background.
7+
* Manages location lifecycle tied to app foreground/background transitions.
88
*
99
* Registered with [ProcessLifecycleOwner] during module initialization.
10-
* [onStop] fires when the app transitions to background — any active
11-
* GPS request is cancelled to avoid unnecessary work while backgrounded.
10+
* - [onStart]: triggers a one-shot location request on the first foreground entry
11+
* when [trackingMode] is [LocationTrackingMode.ON_APP_START].
12+
* - [onStop]: cancels any in-flight GPS request when the app enters background.
1213
*
13-
* No mutable state — thread safety is inherent.
14+
* Thread safety: all lifecycle callbacks are delivered on the main thread
15+
* by [ProcessLifecycleOwner], so no synchronization is needed.
1416
*/
1517
internal class LocationLifecycleObserver(
16-
private val locationServices: LocationServicesImpl
18+
private val locationServices: LocationServicesImpl,
19+
private val trackingMode: LocationTrackingMode
1720
) : DefaultLifecycleObserver {
1821

22+
private var hasRequestedOnStart = false
23+
24+
override fun onStart(owner: LifecycleOwner) {
25+
if (trackingMode == LocationTrackingMode.ON_APP_START && !hasRequestedOnStart) {
26+
hasRequestedOnStart = true
27+
locationServices.requestLocationUpdate()
28+
}
29+
}
30+
1931
override fun onStop(owner: LifecycleOwner) {
2032
locationServices.cancelInFlightRequest()
2133
}

location/src/main/kotlin/io/customer/location/ModuleLocation.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,17 @@ class ModuleLocation @JvmOverloads constructor(
120120
}
121121
}
122122

123-
// Cancel in-flight location requests when the app enters background.
124-
// Lifecycle observer registration must happen on the main thread.
123+
// Register lifecycle observer for background cancellation and ON_APP_START.
124+
// - onStop: cancels in-flight GPS requests when the app enters background.
125+
// - onStart: triggers a one-shot location request on first foreground entry
126+
// when trackingMode is ON_APP_START (guaranteed foreground execution).
127+
// Registration must happen on the main thread.
125128
val mainThreadPoster: MainThreadPoster = HandlerMainThreadPoster()
126129
mainThreadPoster.post {
127130
ProcessLifecycleOwner.get().lifecycle.addObserver(
128-
LocationLifecycleObserver(services)
131+
LocationLifecycleObserver(services, moduleConfig.trackingMode)
129132
)
130133
}
131-
132-
// ON_APP_START: auto-capture location on cold start.
133-
// Routed through LocationServicesImpl so it shares the same job tracking
134-
// and first-wins dedup as user-initiated requests.
135-
// If permissions are denied or services disabled, orchestrator silently no-ops.
136-
if (moduleConfig.trackingMode == LocationTrackingMode.ON_APP_START) {
137-
services.requestLocationUpdate()
138-
}
139134
}
140135

141136
companion object {

0 commit comments

Comments
 (0)