Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import android.os.Build
import com.segment.analytics.kotlin.core.Analytics
Expand Down Expand Up @@ -38,15 +39,7 @@ class PassiveLocationPlugin(val context: Context) : Plugin {
if (haveAnyLocationPermission()
) {


val locationManager =
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager

@SuppressLint("MissingPermission")
// Passive Provider is API level 8
val passiveLastKnownLocation = locationManager.getLastKnownLocation(
LocationManager.PASSIVE_PROVIDER
)
val passiveLastKnownLocation = getLastKnownLocation()

// Build top-level event.context.location object.
put("location", buildJsonObject {
Expand All @@ -65,8 +58,7 @@ class PassiveLocationPlugin(val context: Context) : Plugin {
JsonPrimitive(passiveLastKnownLocation?.bearingAccuracyDegrees)
)
}



if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
put(
"elapsedRealtimeAgeMillis",
Expand All @@ -79,15 +71,24 @@ class PassiveLocationPlugin(val context: Context) : Plugin {
put("isMock", JsonPrimitive(passiveLastKnownLocation?.isMock))
}
})
} else {
// If we don't have permissions then just set event.context.location = "n/a"
put("location", JsonPrimitive("n/a"))
}
}

return event
}

private fun getLastKnownLocation(): Location? {
val locationManager =
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager

@SuppressLint("MissingPermission")
// Passive Provider is API level 8
val passiveLastKnownLocation = locationManager.getLastKnownLocation(
LocationManager.PASSIVE_PROVIDER
)
return passiveLastKnownLocation
}

/**
* Returns true if we have either Fine or Coarse Location Permission.
*/
Expand Down