Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replay time ordering #415

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.6'
radarVersion = '3.18.7-beta.2'
}

String buildNumber = ".${System.currentTimeMillis()}"
Expand Down
12 changes: 5 additions & 7 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,12 @@ 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
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)
params.putOpt("updatedAtMsDiff", updatedAtMsDiff)
params.putOpt("locationMs", locationMs)
}
params.putOpt("foreground", foreground)
params.putOpt("stopped", stopped)
Expand Down Expand Up @@ -396,8 +398,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 +418,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
42 changes: 32 additions & 10 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,6 +80,33 @@ internal open class RadarApiHelper(
}

if (params != null) {
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)
Expand Down