Skip to content

Commit

Permalink
added explanation to billing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
VysotskiVadim committed Jan 16, 2024
1 parent 5a631b6 commit d9d98ea
Showing 1 changed file with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.mapbox.navigation.core.trip.session.NavigationSession
import com.mapbox.navigation.core.trip.session.NavigationSessionState
import com.mapbox.navigation.core.trip.session.NavigationSessionStateObserver
import com.mapbox.navigation.core.trip.session.TripSession
import com.mapbox.navigation.utils.internal.logI
import com.mapbox.navigation.utils.internal.logW
import com.mapbox.turf.TurfConstants.UNIT_METRES
import com.mapbox.turf.TurfMeasurement
Expand Down Expand Up @@ -189,6 +190,7 @@ internal class BillingController(

private companion object {
private const val logCategory = "BillingController"
private const val BILLING_EXPLANATION_CATEGORY = "BillingExplanation"
private const val MAX_WAYPOINTS_DISTANCE_DIFF_METERS = 100.0
}

Expand All @@ -211,18 +213,23 @@ internal class BillingController(
is NavigationSessionState.Idle -> {
getRunningOrPausedSessionSkuId()?.let {
billingService.pauseBillingSession(it)
logI(BILLING_EXPLANATION_CATEGORY) {
"${it.publicName} has been paused because Nav SDK is in Idle state"
}
}
}
is NavigationSessionState.FreeDrive -> {
resumeOrBeginBillingSession(
SessionSKUIdentifier.NAV2_SES_FDTRIP,
validity = TimeUnit.HOURS.toMillis(1) // validity of 1hr
validity = TimeUnit.HOURS.toMillis(1), // validity of 1hr
"Nav SDK is in free drive state"
)
}
is NavigationSessionState.ActiveGuidance -> {
resumeOrBeginBillingSession(
SessionSKUIdentifier.NAV2_SES_TRIP,
validity = 0 // default validity, 12hrs
validity = 0, // default validity, 12hrs
"Nav SDK is in Active Guidance state"
)
}
}
Expand All @@ -243,7 +250,8 @@ internal class BillingController(
}
beginBillingSession(
SessionSKUIdentifier.NAV2_SES_TRIP,
validity = 0 // default validity, 12hrs
validity = 0, // default validity, 12hrs
"Nav SDK switched to the next route leg"
)
}

Expand Down Expand Up @@ -286,10 +294,17 @@ internal class BillingController(
) == BillingSessionStatus.SESSION_PAUSED
beginBillingSession(
SessionSKUIdentifier.NAV2_SES_TRIP,
validity = 0 // default validity, 12hrs
validity = 0, // default validity, 12hrs
"destination has been changed. " +
"Old waypoints: $currentRemainingWaypoints," +
"new waypoints: $newWaypoints"
)
if (wasSessionPaused) {
billingService.pauseBillingSession(SessionSKUIdentifier.NAV2_SES_TRIP)
logI(BILLING_EXPLANATION_CATEGORY) {
"${runningSessionSkuId.publicName} has been paused because " +
"it used to be paused before destinations update"
}
}
}
}
Expand All @@ -300,6 +315,9 @@ internal class BillingController(
arrivalProgressObserver.unregisterObserver(arrivalObserver)
getRunningOrPausedSessionSkuId()?.let {
billingService.stopBillingSession(it)
logI(BILLING_EXPLANATION_CATEGORY) {
"${it.publicName} has been stopped because Nav SDK is destroyed"
}
}
}

Expand All @@ -308,7 +326,8 @@ internal class BillingController(
*/
private fun resumeOrBeginBillingSession(
skuId: SessionSKUIdentifier,
validity: Long
validity: Long,
reason: String
) {
val runningSessionSkuId = getRunningOrPausedSessionSkuId()
if (runningSessionSkuId == skuId) {
Expand All @@ -319,11 +338,18 @@ internal class BillingController(
"Session resumption failed, starting a new one instead.",
logCategory
)
beginBillingSession(skuId, validity)
logI(BILLING_EXPLANATION_CATEGORY) {
"Failed to resume ${skuId.publicName}(${it.message})."
}
beginBillingSession(skuId, validity, reason)
} else {
logI(BILLING_EXPLANATION_CATEGORY) {
"${skuId.publicName} has ben resumed because $reason"
}
}
}
} else {
beginBillingSession(skuId, validity)
beginBillingSession(skuId, validity, reason)
}
}

Expand All @@ -332,11 +358,15 @@ internal class BillingController(
*/
private fun beginBillingSession(
skuId: SessionSKUIdentifier,
validity: Long
validity: Long,
reason: String
) {
val runningSessionSkuId = getRunningOrPausedSessionSkuId()
if (runningSessionSkuId != null) {
billingService.stopBillingSession(runningSessionSkuId)
logI(BILLING_EXPLANATION_CATEGORY) {
"${runningSessionSkuId.publicName} has been stopped because $reason"
}
}
billingService.beginBillingSession(
accessToken,
Expand All @@ -347,6 +377,9 @@ internal class BillingController(
},
validity
)
logI(BILLING_EXPLANATION_CATEGORY) {
"${skuId.publicName} has been started because $reason"
}
}

private fun getRunningOrPausedSessionSkuId(): SessionSKUIdentifier? {
Expand Down Expand Up @@ -438,3 +471,8 @@ internal class BillingController(
}

private data class Waypoint(val index: Int, val point: Point)

private val SessionSKUIdentifier.publicName get() = when (this) {
SessionSKUIdentifier.NAV2_SES_TRIP -> "Active Guidance Trip Session"
SessionSKUIdentifier.NAV2_SES_FDTRIP -> "Free Drive Trip Session"
}

0 comments on commit d9d98ea

Please sign in to comment.