Skip to content

Commit d90110d

Browse files
authored
Geofence Android demo (#2491)
1 parent 405c23e commit d90110d

File tree

8 files changed

+966
-14
lines changed

8 files changed

+966
-14
lines changed

app/permission.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[
2+
"android.permission.ACCESS_BACKGROUND_LOCATION",
23
"android.permission.ACCESS_COARSE_LOCATION",
34
"android.permission.ACCESS_FINE_LOCATION",
45
"android.permission.ACCESS_NETWORK_STATE",
56
"android.permission.ACCESS_WIFI_STATE",
67
"android.permission.INTERNET",
8+
"android.permission.POST_NOTIFICATIONS",
79
"android.permission.REORDER_TASKS",
810
"com.mapbox.maps.testapp.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
911
]

app/src/main/AndroidManifest.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
66
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
7+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
8+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
79

810
<application
911
android:name=".MapboxApplication"
@@ -400,6 +402,19 @@
400402
android:name="android.support.PARENT_ACTIVITY"
401403
android:value=".ExampleOverviewActivity" />
402404
</activity>
405+
<activity
406+
android:name=".examples.geofence.GeofencingActivity"
407+
android:description="@string/description_geofencing"
408+
android:exported="true"
409+
android:label="@string/activity_geofencing"
410+
android:launchMode="singleTask">
411+
<meta-data
412+
android:name="@string/category"
413+
android:value="@string/category_location" />
414+
<meta-data
415+
android:name="android.support.PARENT_ACTIVITY"
416+
android:value=".ExampleOverviewActivity" />
417+
</activity>
403418
<activity
404419
android:name=".examples.camera.RestrictBoundsActivity"
405420
android:description="@string/description_restrict_bounds"

app/src/main/java/com/mapbox/maps/testapp/MapboxApplication.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,64 @@ package com.mapbox.maps.testapp
22

33
import android.os.StrictMode
44
import androidx.multidex.MultiDexApplication
5+
import com.mapbox.android.core.permissions.PermissionsManager
6+
import com.mapbox.common.experimental.geofencing.GeofencingError
7+
import com.mapbox.common.experimental.geofencing.GeofencingEvent
8+
import com.mapbox.common.experimental.geofencing.GeofencingFactory
9+
import com.mapbox.common.experimental.geofencing.GeofencingObserver
10+
import com.mapbox.maps.logD
11+
import com.mapbox.maps.logW
12+
import com.mapbox.maps.testapp.examples.geofence.GeofencingActivity
513

614
/**
715
* Application class of the test application.
816
**/
917
class MapboxApplication : MultiDexApplication() {
1018

19+
private val geofencingObserver: GeofencingObserver = object : GeofencingObserver {
20+
21+
override fun onEntry(event: GeofencingEvent) {
22+
GeofencingActivity.showNotification(
23+
this@MapboxApplication,
24+
"Entry into feature id = ${event.feature.id()} at ${event.timestamp}",
25+
event.feature.id(),
26+
GeofencingActivity.NOTIFICATION_FEATURE_ENTRY
27+
)
28+
}
29+
30+
override fun onExit(event: GeofencingEvent) {
31+
GeofencingActivity.showNotification(
32+
this@MapboxApplication,
33+
"Exit from feature id = ${event.feature.id()} at ${event.timestamp}",
34+
event.feature.id(),
35+
GeofencingActivity.NOTIFICATION_FEATURE_EXIT
36+
)
37+
}
38+
39+
override fun onDwell(event: GeofencingEvent) {
40+
GeofencingActivity.showNotification(
41+
this@MapboxApplication,
42+
"Dwell into feature id = ${event.feature.id()} at ${event.timestamp}",
43+
event.feature.id(),
44+
GeofencingActivity.NOTIFICATION_FEATURE_DWELL
45+
)
46+
}
47+
48+
override fun onError(error: GeofencingError) {
49+
logD("MapboxApplication", "onError() called with: error = $error")
50+
}
51+
}
52+
53+
// TODO: temporary workaround to avoid double adding of listener if we don't
54+
// have location permissions on the start of the app
55+
private var isObserverAdded: Boolean = false
56+
1157
override fun onCreate() {
1258
super.onCreate()
1359
initializeStrictMode()
60+
if (ENABLE_BACKGROUND_GEOFENCING) {
61+
registerGeofencingObserver()
62+
}
1463
}
1564

1665
private fun initializeStrictMode() {
@@ -27,4 +76,25 @@ class MapboxApplication : MultiDexApplication() {
2776
.build()
2877
)
2978
}
79+
80+
fun registerGeofencingObserver() {
81+
if (PermissionsManager.areLocationPermissionsGranted(this) && !isObserverAdded) {
82+
val geofencing = GeofencingFactory.getOrCreate()
83+
geofencing.addObserver(geofencingObserver) { it ->
84+
it.error?.let {
85+
logW("MapboxApplication", "Failed to registerGeofencingObserver: ${it.message}")
86+
}
87+
}
88+
isObserverAdded = true
89+
}
90+
}
91+
92+
companion object {
93+
94+
/**
95+
* Flag to showcase background behavior of the geofence engine. When enabled, notifications will
96+
* be created for the different geofencing events.
97+
*/
98+
const val ENABLE_BACKGROUND_GEOFENCING = true
99+
}
30100
}

0 commit comments

Comments
 (0)