Skip to content

Commit

Permalink
Replay time ordering (#415)
Browse files Browse the repository at this point in the history
* API client

* fix version gating for apiclient and implement editing of updatedAtMsDiff in apihelper

* Fixes to api helper

* 3.18.6-beta.1

* add version gate

* fix sending up replays

* always put updatedAtMsDiff

* Update sdk/src/main/java/io/radar/sdk/RadarApiHelper.kt

Co-authored-by: Tim Julien <tim@radar.io>

* nit: spacing adjustments

* Put behind a feature setting

* Update sdk config test

* remove beta tag

---------

Co-authored-by: Kenny Hu <kenny.hu@radar.com>
Co-authored-by: KennyHuRadar <139801512+KennyHuRadar@users.noreply.github.com>
Co-authored-by: Tim Julien <tim@radar.io>
  • Loading branch information
4 people authored Dec 9, 2024
1 parent bec3c9e commit cca1abf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
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.8'
radarVersion = '3.18.9'
}

String buildNumber = ".${System.currentTimeMillis()}"
Expand Down
16 changes: 8 additions & 8 deletions sdk/src/main/java/io/radar/sdk/RadarApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.radar.sdk

import android.content.Context
import android.location.Location
import android.net.Uri
import android.os.Build
import android.os.SystemClock
import io.radar.sdk.Radar.RadarAddressVerificationStatus
Expand Down Expand Up @@ -305,9 +304,14 @@ internal class RadarApiClient(
params.putOpt("courseAccuracy", location.bearingAccuracyDegrees)
}
}
if (!foreground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
val updatedAtMsDiff = (SystemClock.elapsedRealtimeNanos() - location.elapsedRealtimeNanos) / 1000000
params.putOpt("updatedAtMsDiff", updatedAtMsDiff)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
val nowMs = SystemClock.elapsedRealtimeNanos() / 1000000
val locationMs = location.elapsedRealtimeNanos / 1000000
val updatedAtMsDiff = (nowMs - locationMs)
if (RadarSettings.getSdkConfiguration(context).useForegroundLocationUpdatedAtMsDiff || !foreground) {
params.putOpt("updatedAtMsDiff", updatedAtMsDiff)
}
params.putOpt("locationMs", locationMs)
}
params.putOpt("foreground", foreground)
params.putOpt("stopped", stopped)
Expand Down Expand Up @@ -396,8 +400,6 @@ internal class RadarApiClient(

val hasReplays = Radar.hasReplays()
var requestParams = params
val nowMS = System.currentTimeMillis()

// before we track, check if replays need to sync
val replaying = options.replay == RadarTrackingOptions.RadarTrackingOptionsReplay.ALL && hasReplays && !verified
if (replaying) {
Expand All @@ -418,8 +420,6 @@ internal class RadarApiClient(
if (status != RadarStatus.SUCCESS || res == null) {
if (options.replay == RadarTrackingOptions.RadarTrackingOptionsReplay.ALL) {
params.putOpt("replayed", true)
params.putOpt("updatedAtMs", nowMS)
params.remove("updatedAtMsDiff")
Radar.addReplay(params)
} else if (options.replay == RadarTrackingOptions.RadarTrackingOptionsReplay.STOPS && stopped && !(source == RadarLocationSource.FOREGROUND_LOCATION || source == RadarLocationSource.BACKGROUND_LOCATION)) {
RadarState.setLastFailedStoppedLocation(context, location)
Expand Down
45 changes: 34 additions & 11 deletions sdk/src/main/java/io/radar/sdk/RadarApiHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ package io.radar.sdk

import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import io.radar.sdk.Radar.RadarLogType
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.io.BufferedInputStream
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStreamWriter
import java.net.URL
import java.security.KeyStore
import java.security.cert.Certificate
import java.security.cert.CertificateFactory
import java.util.*
import java.util.Scanner
import java.util.concurrent.Executors
import io.radar.sdk.Radar.RadarLogType
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManagerFactory

internal open class RadarApiHelper(
private var logger: RadarLogger? = null
Expand Down Expand Up @@ -85,8 +80,36 @@ internal open class RadarApiHelper(
}

if (params != null) {
urlConnection.doOutput = true
val prevUpdatedAtMsDiff = params.optLong("updatedAtMsDiff", -1L)
val replays = params.optJSONArray("replays")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && (prevUpdatedAtMsDiff != -1L || replays != null)) {
val nowMs = SystemClock.elapsedRealtimeNanos() / 1000000
val locationMs = params.optLong("locationMs", -1L)

if (prevUpdatedAtMsDiff != -1L && locationMs != -1L){
val updatedAtMsDiff = nowMs - locationMs
params.put("updatedAtMsDiff", updatedAtMsDiff)
}

if (replays != null) {
val updatedReplays = mutableListOf<JSONObject>()
for (i in 0 until replays.length()) {
val replay = replays.optJSONObject(i)
replay?.let {
val replayLocationMs = it.optLong("locationMs", -1L)
if (replayLocationMs != -1L) {
val replayUpdatedAtMsDiff = nowMs - replayLocationMs
it.put("updatedAtMsDiff", replayUpdatedAtMsDiff)
}
updatedReplays.add(it)
}
}
params.put("replays", JSONArray(updatedReplays))
}
}

urlConnection.doOutput = true
val outputStreamWriter = OutputStreamWriter(urlConnection.outputStream)
outputStreamWriter.write(params.toString())
outputStreamWriter.close()
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/main/java/io/radar/sdk/model/RadarSdkConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal data class RadarSdkConfiguration(
val trackOnceOnAppOpen: Boolean,
val useLocationMetadata: Boolean,
val useOpenedAppConversion: Boolean = false,
val useForegroundLocationUpdatedAtMsDiff: Boolean = false,
) {
companion object {
private const val MAX_CONCURRENT_JOBS = "maxConcurrentJobs"
Expand All @@ -35,6 +36,7 @@ internal data class RadarSdkConfiguration(
private const val TRACK_ONCE_ON_APP_OPEN = "trackOnceOnAppOpen"
private const val USE_LOCATION_METADATA = "useLocationMetadata"
private const val USE_OPENED_APP_CONVERSION = "useOpenedAppConversion"
private const val USE_FOREGROUND_LOCATION_UPDATED_AT_MS_DIFF = "useForegroundLocationUpdatedAtMsDiff"


fun fromJson(json: JSONObject?): RadarSdkConfiguration {
Expand All @@ -53,6 +55,7 @@ internal data class RadarSdkConfiguration(
config.optBoolean(TRACK_ONCE_ON_APP_OPEN, false),
config.optBoolean(USE_LOCATION_METADATA, false),
config.optBoolean(USE_OPENED_APP_CONVERSION, true),
config.optBoolean(USE_FOREGROUND_LOCATION_UPDATED_AT_MS_DIFF, false),
)
}

Expand Down Expand Up @@ -82,6 +85,7 @@ internal data class RadarSdkConfiguration(
putOpt(TRACK_ONCE_ON_APP_OPEN, trackOnceOnAppOpen)
putOpt(USE_LOCATION_METADATA, useLocationMetadata)
putOpt(USE_OPENED_APP_CONVERSION, useOpenedAppConversion)
putOpt(USE_FOREGROUND_LOCATION_UPDATED_AT_MS_DIFF, useForegroundLocationUpdatedAtMsDiff)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RadarSdkConfigurationTest {
private var trackOnceOnAppOpen = false
private var useLocationMetadata = false
private var useOpenedAppConversion = false
private var useForegroundLocationUpdatedAtMsDiff = false

@Before
fun setUp() {
Expand All @@ -46,7 +47,8 @@ class RadarSdkConfigurationTest {
"startTrackingOnInitialize":$startTrackingOnInitialize,
"trackOnceOnAppOpen":$trackOnceOnAppOpen,
"useLocationMetadata":$useLocationMetadata,
"useOpenedAppConversion":$useOpenedAppConversion
"useOpenedAppConversion":$useOpenedAppConversion,
"useForegroundLocationUpdatedAtMsDiff":$useForegroundLocationUpdatedAtMsDiff
}""".trimIndent()
}

Expand All @@ -65,7 +67,8 @@ class RadarSdkConfigurationTest {
startTrackingOnInitialize,
trackOnceOnAppOpen,
useLocationMetadata,
useOpenedAppConversion
useOpenedAppConversion,
useForegroundLocationUpdatedAtMsDiff
).toJson().toString()
)
}
Expand All @@ -84,6 +87,7 @@ class RadarSdkConfigurationTest {
assertEquals(trackOnceOnAppOpen, settings.trackOnceOnAppOpen)
assertEquals(useLocationMetadata, settings.useLocationMetadata)
assertEquals(useOpenedAppConversion, settings.useOpenedAppConversion)
assertEquals(useForegroundLocationUpdatedAtMsDiff, settings.useForegroundLocationUpdatedAtMsDiff)
}

@Test
Expand All @@ -100,6 +104,7 @@ class RadarSdkConfigurationTest {
assertFalse(settings.trackOnceOnAppOpen)
assertFalse(settings.useLocationMetadata)
assertTrue(settings.useOpenedAppConversion)
assertFalse(settings.useForegroundLocationUpdatedAtMsDiff)
}

private fun String.removeWhitespace(): String = replace("\\s".toRegex(), "")
Expand Down

0 comments on commit cca1abf

Please # to comment.