Skip to content

Commit

Permalink
Update passive location plugin 2 (#198)
Browse files Browse the repository at this point in the history
* Don't set the event.context.location property to anything if we don't have location permissions.

* Refactor getting last location into function.
  • Loading branch information
didiergarcia authored Nov 20, 2023
1 parent fd36a96 commit 3a65ff2
Showing 1 changed file with 15 additions and 11 deletions.
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 @@ -85,6 +77,18 @@ class PassiveLocationPlugin(val context: Context) : Plugin {
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

0 comments on commit 3a65ff2

Please sign in to comment.