From 0ac40232fddece2d7732276726f3d1975929c8fa Mon Sep 17 00:00:00 2001 From: Didier Garcia Date: Fri, 27 Oct 2023 16:58:04 -0400 Subject: [PATCH 1/2] Don't set the event.context.location property to anything if we don't have location permissions. --- .../segment/analytics/next/plugins/PassiveLocationPlugin.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt b/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt index 495c2e59..e4cc622a 100644 --- a/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt +++ b/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt @@ -79,9 +79,6 @@ 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")) } } From 12fa57b1feb86389425d361f7e63b19554e5634b Mon Sep 17 00:00:00 2001 From: Didier Garcia Date: Mon, 20 Nov 2023 12:29:23 -0500 Subject: [PATCH 2/2] Refactor getting last location into function. --- .../next/plugins/PassiveLocationPlugin.kt | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt b/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt index e4cc622a..4b36ad5a 100644 --- a/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt +++ b/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt @@ -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 @@ -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 { @@ -65,8 +58,7 @@ class PassiveLocationPlugin(val context: Context) : Plugin { JsonPrimitive(passiveLastKnownLocation?.bearingAccuracyDegrees) ) } - - + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { put( "elapsedRealtimeAgeMillis", @@ -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. */