Skip to content

Commit 95c00bf

Browse files
authored
Update comments on the PassiveLocationPlugin. (#191)
1 parent c4e4d13 commit 95c00bf

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

samples/kotlin-android-app/src/main/java/com/segment/analytics/next/plugins/PassiveLocationPlugin.kt

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.segment.analytics.next.plugins
22

33
import android.Manifest
4+
import android.annotation.SuppressLint
45
import android.content.Context
56
import android.content.pm.PackageManager
67
import android.location.LocationManager
@@ -12,34 +13,42 @@ import kotlinx.serialization.json.JsonPrimitive
1213
import kotlinx.serialization.json.buildJsonObject
1314
import androidx.core.app.ActivityCompat
1415

15-
class PassiveLocationPlugin(val context: Context): Plugin {
16+
/**
17+
* The PassiveLocationPlugin will add location information to `event.context.location` if the host
18+
* app is granted Fine or Coarse location.
19+
*
20+
* This plugin will not cause the app the request permission, the host app must implement that logic.
21+
*/
22+
class PassiveLocationPlugin(val context: Context) : Plugin {
1623
override lateinit var analytics: Analytics
1724
override val type: Plugin.Type = Plugin.Type.Enrichment
1825

26+
1927
override fun execute(event: BaseEvent): BaseEvent? {
2028

29+
// Update the context property
2130
event.context = buildJsonObject {
2231

32+
// Add all existing context properties
2333
event.context.forEach { (key, value) ->
2434
put(key, value)
2535
}
2636

27-
if (ActivityCompat.checkSelfPermission(
28-
context,
29-
Manifest.permission.ACCESS_FINE_LOCATION
30-
) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
31-
context,
32-
Manifest.permission.ACCESS_COARSE_LOCATION
33-
) == PackageManager.PERMISSION_GRANTED
37+
// If we have Location Permission (Fine or Coarse)
38+
if (haveAnyLocationPermission()
3439
) {
40+
41+
3542
val locationManager =
3643
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
3744

45+
@SuppressLint("MissingPermission")
3846
// Passive Provider is API level 8
3947
val passiveLastKnownLocation = locationManager.getLastKnownLocation(
4048
LocationManager.PASSIVE_PROVIDER
4149
)
4250

51+
// Build top-level event.context.location object.
4352
put("location", buildJsonObject {
4453
put("lat", JsonPrimitive(passiveLastKnownLocation?.latitude))
4554
put("lon", JsonPrimitive(passiveLastKnownLocation?.longitude))
@@ -71,12 +80,22 @@ class PassiveLocationPlugin(val context: Context): Plugin {
7180
}
7281
})
7382
} else {
83+
// If we don't have permissions then just set event.context.location = "n/a"
7484
put("location", JsonPrimitive("n/a"))
7585
}
76-
7786
}
7887

79-
8088
return event
8189
}
90+
91+
/**
92+
* Returns true if we have either Fine or Coarse Location Permission.
93+
*/
94+
private fun haveAnyLocationPermission() = ActivityCompat.checkSelfPermission(
95+
context,
96+
Manifest.permission.ACCESS_FINE_LOCATION
97+
) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
98+
context,
99+
Manifest.permission.ACCESS_COARSE_LOCATION
100+
) == PackageManager.PERMISSION_GRANTED
82101
}

0 commit comments

Comments
 (0)