@@ -2,15 +2,64 @@ package com.mapbox.maps.testapp
22
33import android.os.StrictMode
44import 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 **/
917class 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