@@ -4,6 +4,7 @@ import android.Manifest
4
4
import android.annotation.SuppressLint
5
5
import android.content.Context
6
6
import android.content.pm.PackageManager
7
+ import android.location.Location
7
8
import android.location.LocationManager
8
9
import android.os.Build
9
10
import com.segment.analytics.kotlin.core.Analytics
@@ -38,15 +39,7 @@ class PassiveLocationPlugin(val context: Context) : Plugin {
38
39
if (haveAnyLocationPermission()
39
40
) {
40
41
41
-
42
- val locationManager =
43
- context.getSystemService(Context .LOCATION_SERVICE ) as LocationManager
44
-
45
- @SuppressLint(" MissingPermission" )
46
- // Passive Provider is API level 8
47
- val passiveLastKnownLocation = locationManager.getLastKnownLocation(
48
- LocationManager .PASSIVE_PROVIDER
49
- )
42
+ val passiveLastKnownLocation = getLastKnownLocation()
50
43
51
44
// Build top-level event.context.location object.
52
45
put(" location" , buildJsonObject {
@@ -65,8 +58,7 @@ class PassiveLocationPlugin(val context: Context) : Plugin {
65
58
JsonPrimitive (passiveLastKnownLocation?.bearingAccuracyDegrees)
66
59
)
67
60
}
68
-
69
-
61
+
70
62
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
71
63
put(
72
64
" elapsedRealtimeAgeMillis" ,
@@ -85,6 +77,18 @@ class PassiveLocationPlugin(val context: Context) : Plugin {
85
77
return event
86
78
}
87
79
80
+ private fun getLastKnownLocation (): Location ? {
81
+ val locationManager =
82
+ context.getSystemService(Context .LOCATION_SERVICE ) as LocationManager
83
+
84
+ @SuppressLint(" MissingPermission" )
85
+ // Passive Provider is API level 8
86
+ val passiveLastKnownLocation = locationManager.getLastKnownLocation(
87
+ LocationManager .PASSIVE_PROVIDER
88
+ )
89
+ return passiveLastKnownLocation
90
+ }
91
+
88
92
/* *
89
93
* Returns true if we have either Fine or Coarse Location Permission.
90
94
*/
0 commit comments