Skip to content

Commit

Permalink
Geofence Android demo (#2491)
Browse files Browse the repository at this point in the history
  • Loading branch information
flasher297 authored Oct 9, 2024
1 parent 405c23e commit d90110d
Show file tree
Hide file tree
Showing 8 changed files with 966 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/permission.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[
"android.permission.ACCESS_BACKGROUND_LOCATION",
"android.permission.ACCESS_COARSE_LOCATION",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.ACCESS_NETWORK_STATE",
"android.permission.ACCESS_WIFI_STATE",
"android.permission.INTERNET",
"android.permission.POST_NOTIFICATIONS",
"android.permission.REORDER_TASKS",
"com.mapbox.maps.testapp.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
]
15 changes: 15 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<application
android:name=".MapboxApplication"
Expand Down Expand Up @@ -400,6 +402,19 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".ExampleOverviewActivity" />
</activity>
<activity
android:name=".examples.geofence.GeofencingActivity"
android:description="@string/description_geofencing"
android:exported="true"
android:label="@string/activity_geofencing"
android:launchMode="singleTask">
<meta-data
android:name="@string/category"
android:value="@string/category_location" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ExampleOverviewActivity" />
</activity>
<activity
android:name=".examples.camera.RestrictBoundsActivity"
android:description="@string/description_restrict_bounds"
Expand Down
70 changes: 70 additions & 0 deletions app/src/main/java/com/mapbox/maps/testapp/MapboxApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,64 @@ package com.mapbox.maps.testapp

import android.os.StrictMode
import androidx.multidex.MultiDexApplication
import com.mapbox.android.core.permissions.PermissionsManager
import com.mapbox.common.experimental.geofencing.GeofencingError
import com.mapbox.common.experimental.geofencing.GeofencingEvent
import com.mapbox.common.experimental.geofencing.GeofencingFactory
import com.mapbox.common.experimental.geofencing.GeofencingObserver
import com.mapbox.maps.logD
import com.mapbox.maps.logW
import com.mapbox.maps.testapp.examples.geofence.GeofencingActivity

/**
* Application class of the test application.
**/
class MapboxApplication : MultiDexApplication() {

private val geofencingObserver: GeofencingObserver = object : GeofencingObserver {

override fun onEntry(event: GeofencingEvent) {
GeofencingActivity.showNotification(
this@MapboxApplication,
"Entry into feature id = ${event.feature.id()} at ${event.timestamp}",
event.feature.id(),
GeofencingActivity.NOTIFICATION_FEATURE_ENTRY
)
}

override fun onExit(event: GeofencingEvent) {
GeofencingActivity.showNotification(
this@MapboxApplication,
"Exit from feature id = ${event.feature.id()} at ${event.timestamp}",
event.feature.id(),
GeofencingActivity.NOTIFICATION_FEATURE_EXIT
)
}

override fun onDwell(event: GeofencingEvent) {
GeofencingActivity.showNotification(
this@MapboxApplication,
"Dwell into feature id = ${event.feature.id()} at ${event.timestamp}",
event.feature.id(),
GeofencingActivity.NOTIFICATION_FEATURE_DWELL
)
}

override fun onError(error: GeofencingError) {
logD("MapboxApplication", "onError() called with: error = $error")
}
}

// TODO: temporary workaround to avoid double adding of listener if we don't
// have location permissions on the start of the app
private var isObserverAdded: Boolean = false

override fun onCreate() {
super.onCreate()
initializeStrictMode()
if (ENABLE_BACKGROUND_GEOFENCING) {
registerGeofencingObserver()
}
}

private fun initializeStrictMode() {
Expand All @@ -27,4 +76,25 @@ class MapboxApplication : MultiDexApplication() {
.build()
)
}

fun registerGeofencingObserver() {
if (PermissionsManager.areLocationPermissionsGranted(this) && !isObserverAdded) {
val geofencing = GeofencingFactory.getOrCreate()
geofencing.addObserver(geofencingObserver) { it ->
it.error?.let {
logW("MapboxApplication", "Failed to registerGeofencingObserver: ${it.message}")
}
}
isObserverAdded = true
}
}

companion object {

/**
* Flag to showcase background behavior of the geofence engine. When enabled, notifications will
* be created for the different geofencing events.
*/
const val ENABLE_BACKGROUND_GEOFENCING = true
}
}
Loading

0 comments on commit d90110d

Please sign in to comment.