Skip to content

Commit

Permalink
Merge pull request #409 from radarlabs/start-tracking-verified-track-…
Browse files Browse the repository at this point in the history
…call

Fix startTrackingVerified() recursive track calls, stopTrackingVerified() improvements
  • Loading branch information
nickpatrick authored Oct 22, 2024
2 parents 8a4fd28 + ae9ed1f commit 2627962
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 47 deletions.
24 changes: 24 additions & 0 deletions example/src/main/java/io/radar/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ class MainActivity : AppCompatActivity() {
}
}

createButton("startTrackingVerified") {
Radar.startTrackingVerified(60, false)
}

createButton("stopTrackingVerified") {
Radar.stopTrackingVerified()
}

createButton("getVerifiedLocationToken") {
Radar.getVerifiedLocationToken { status, token ->
Log.v("example", "GetVerifiedLocationToken: status = $status; token = ${token?.toJson()}")
}
}

createButton("trackVerified") {
Radar.trackVerified(false) { status, token ->
Log.v("example", "TrackVerified: status = $status; token = ${token?.toJson()}")
}
}

createButton("setExpectedJurisdiction") {
Radar.setExpectedJurisdiction("US", "CA")
}
Expand All @@ -127,6 +147,10 @@ class MainActivity : AppCompatActivity() {
Radar.startTracking(options)
}

createButton("stopTracking") {
Radar.stopTracking()
}

createButton("getContext") {
Radar.getContext { status, location, context ->
Log.v(
Expand Down
2 changes: 1 addition & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ apply plugin: "org.jetbrains.dokka"
apply plugin: 'io.radar.mvnpublish'

ext {
radarVersion = '3.18.3'
radarVersion = '3.18.4'
}

String buildNumber = ".${System.currentTimeMillis()}"
Expand Down
105 changes: 59 additions & 46 deletions sdk/src/main/java/io/radar/sdk/RadarVerificationManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class RadarVerificationManager(
private var networkCallback: ConnectivityManager.NetworkCallback? = null
private var started = false
private var scheduled = false
private var runnable: Runnable? = null
private var lastToken: RadarVerifiedLocationToken? = null
private var lastTokenElapsedRealtime: Long = 0L
private var lastTokenBeacons: Boolean = false
Expand Down Expand Up @@ -221,62 +222,70 @@ internal class RadarVerificationManager(
})
}

fun startTrackingVerified(interval: Int, beacons: Boolean) {
private fun callTrackVerified(interval: Int, beacons: Boolean) {
val verificationManager = this

verificationManager.started = true
verificationManager.scheduled = false

val trackVerified = { ->
verificationManager.trackVerified(beacons, object : Radar.RadarTrackVerifiedCallback {
override fun onComplete(
status: Radar.RadarStatus,
token: RadarVerifiedLocationToken?
) {
var expiresIn = 0
var minInterval: Int = interval
verificationManager.trackVerified(beacons, object : Radar.RadarTrackVerifiedCallback {
override fun onComplete(
status: Radar.RadarStatus,
token: RadarVerifiedLocationToken?
) {
var expiresIn = 0
var minInterval: Int = interval

token?.let {
expiresIn = it.expiresIn
token?.let {
expiresIn = it.expiresIn

// if expiresIn is shorter than interval, override interval
minInterval = minOf(it.expiresIn, interval)
}
// if expiresIn is shorter than interval, override interval
minInterval = minOf(it.expiresIn, interval)
}

// re-request early to maximize the likelihood that a cached token is available
if (minInterval > 20) {
minInterval -= 10
}
// re-request early to maximize the likelihood that a cached token is available
if (minInterval > 20) {
minInterval -= 10
}

// min interval is 10 seconds
if (minInterval < 10) {
minInterval = 10;
}
// min interval is 10 seconds
if (minInterval < 10) {
minInterval = 10;
}

if (verificationManager.scheduled) {
verificationManager.logger.d("Token request already scheduled")
if (verificationManager.scheduled) {
verificationManager.logger.d("Token request already scheduled")

return
}
return
}

verificationManager.logger.d("Requesting token again in $minInterval seconds | minInterval = $minInterval; expiresIn = $expiresIn; interval = $interval")
verificationManager.logger.d("Requesting token again in $minInterval seconds | minInterval = $minInterval; expiresIn = $expiresIn; interval = $interval")

verificationManager.scheduled = true
verificationManager.scheduled = true

handler.postDelayed(object : Runnable {
override fun run() {
if (verificationManager.started) {
verificationManager.logger.d("Token request interval fired")
if (runnable == null) {
runnable = Runnable {
if (verificationManager.started) {
verificationManager.logger.d("Token request interval fired")

trackVerified()
callTrackVerified(interval, beacons)

verificationManager.scheduled = false
}
verificationManager.scheduled = false
}
}, minInterval * 1000L)
}
}
})
}

runnable?.let {
handler.postDelayed(it, minInterval * 1000L)
}
}
})
}

fun startTrackingVerified(interval: Int, beacons: Boolean) {
val verificationManager = this

verificationManager.stopTrackingVerified()

verificationManager.started = true
verificationManager.scheduled = false

val request = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
Expand Down Expand Up @@ -305,7 +314,7 @@ internal class RadarVerificationManager(
verificationManager.lastIPs = ips

if (changed) {
trackVerified()
callTrackVerified(interval, beacons)
}
}

Expand All @@ -327,7 +336,7 @@ internal class RadarVerificationManager(
connectivityManager.registerNetworkCallback(request, it)
}

trackVerified()
callTrackVerified(interval, beacons)
}

fun stopTrackingVerified() {
Expand All @@ -336,15 +345,19 @@ internal class RadarVerificationManager(
networkCallback?.let {
connectivityManager.unregisterNetworkCallback(it)
}

runnable?.let {
handler.removeCallbacks(it)
}
}

fun getVerifiedLocationToken(callback: Radar.RadarTrackVerifiedCallback? = null) {
val lastTokenElapsed = (SystemClock.elapsedRealtime() - this.lastTokenElapsedRealtime) / 1000

if (this.lastToken != null) {
this.lastToken?.let {
if (lastTokenElapsed < it.expiresIn) {
Radar.logger.d("Last token valid | lastToken.expiresIn = ${it.expiresIn}; lastTokenElapsed = $lastTokenElapsed")
if (lastTokenElapsed < it.expiresIn && it.passed) {
Radar.logger.d("Last token valid | lastToken.expiresIn = ${it.expiresIn}; lastTokenElapsed = $lastTokenElapsed; lastToken.passed = ${it.passed}")

Radar.flushLogs()

Expand All @@ -353,7 +366,7 @@ internal class RadarVerificationManager(
return
}

Radar.logger.d("Last token invalid | lastToken.expiresIn = ${it.expiresIn}; lastTokenElapsed = $lastTokenElapsed")
Radar.logger.d("Last token invalid | lastToken.expiresIn = ${it.expiresIn}; lastTokenElapsed = $lastTokenElapsed; lastToken.passed = ${it.passed}")
}
} else {
Radar.logger.d("No last token")
Expand Down

0 comments on commit 2627962

Please sign in to comment.