Skip to content

Commit

Permalink
Merge pull request #411 from skedgo/bugfix/20464-destination-notifica…
Browse files Browse the repository at this point in the history
…tion-fix

[20464] Destination notification fix
  • Loading branch information
MichaelReyes authored Feb 7, 2024
2 parents d33cf63 + 27d6563 commit 95abb30
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ data class Geofence(
) {
var timeline: Long = -1L

fun computeAndSetTimeline(tripEndDateTimeInMillis: Long): Long {
fun computeAndSetTimeline(tripEndDateTimeInMillis: Long) {
val currentTimeInMillis = System.currentTimeMillis()
return tripEndDateTimeInMillis - currentTimeInMillis
timeline = tripEndDateTimeInMillis - currentTimeInMillis
}
}

Expand Down
2 changes: 2 additions & 0 deletions TripKitAndroid/src/main/java/com/skedgo/tripkit/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ public interface Configs {

@Value.Default public default boolean hideFavorites() { return false;}

@Value.Default public default boolean showGeofences() { return false;}

}
10 changes: 10 additions & 0 deletions TripKitAndroid/src/main/java/com/skedgo/tripkit/LocationUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.skedgo.tripkit

import android.content.Context
import android.location.LocationManager

fun Context.checkIfLocationProviderIsEnabled(): Boolean {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,45 @@ object GeoLocation {
}
private val currentGeofenceList = mutableListOf<com.google.android.gms.location.Geofence>()
private var geofencePendingIntent: PendingIntent? = null
private val gson: Gson by lazy {
Gson()
}

fun init(context: Context) {
this.context = context
}

@SuppressLint("MissingPermission")
fun createGeoFences(trip: Trip, geofences: List<Geofence>) {
fun createGeoFences(
trip: Trip,
geofences: List<Geofence>,
addGeofenceListener: (Boolean) -> Unit
) {
val gsmGeofences = mutableListOf<com.google.android.gms.location.Geofence>()
gsmGeofences.addAll(
geofences.map { it.toGsmGeofence() }
)
gsmGeofences.addAll(geofences.map { it.toGsmGeofence() })

currentGeofenceList.clear()
currentGeofenceList.addAll(gsmGeofences)

val bundle = Bundle()
bundle.putString(GeofenceBroadcastReceiver.EXTRA_GEOFENCES, Gson().toJson(geofences))
bundle.putString(GeofenceBroadcastReceiver.EXTRA_TRIP, Gson().toJson(trip))
val data = mutableMapOf(
GeofenceBroadcastReceiver.EXTRA_GEOFENCES to gson.toJson(geofences),
GeofenceBroadcastReceiver.EXTRA_TRIP to gson.toJson(trip)
)
trip.group?.let {
bundle.putString(GeofenceBroadcastReceiver.EXTRA_TRIP_GROUP_UUID, it.uuid())
data.put(GeofenceBroadcastReceiver.EXTRA_TRIP_GROUP_UUID, it.uuid())
}
geofencePendingIntent = GeofenceBroadcastReceiver.getPendingIntent(context, bundle)
geofencePendingIntent = GeofenceBroadcastReceiver.getPendingIntent(context, data)

createGeoFencingRequest()?.let { request ->
if (currentGeofenceList.isNotEmpty()) {
removeGeoFences(onRemoveCallback = {
geofencingClient.addGeofences(request, geofencePendingIntent).run {
addOnSuccessListener {
addGeofenceListener.invoke(true)
Log.e(TAG, "Geofence added successfully")
}
addOnFailureListener {
addGeofenceListener.invoke(false)
Log.e(TAG, "Geofence add failed: ${it.message}")
currentGeofenceList.clear()
if (BuildConfig.DEBUG)
Expand All @@ -66,7 +74,6 @@ object GeoLocation {

private fun createGeoFencingRequest(): GeofencingRequest? {
if (currentGeofenceList.isNotEmpty()) {
Log.e("GeoLocation", "creating geofence with ${currentGeofenceList.size} features")
return GeofencingRequest.Builder().apply {
setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
addGeofences(currentGeofenceList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationCompat
Expand All @@ -28,17 +27,18 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
const val EXTRA_TRIP_GROUP_UUID = "EXTRA_TRIP_GROUP_UUID"
const val NOTIFICATION_VEHICLE_APPROACHING_NOTIFICATION_ID = 9002

fun getPendingIntent(context: Context, bundle: Bundle? = null): PendingIntent {
fun getPendingIntent(context: Context, dataMap: Map<String, String>? = null): PendingIntent {
val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
bundle?.let { intent.putExtras(bundle) }
dataMap?.forEach {
intent.putExtra(it.key, it.value)
}
intent.action = ACTION_GEOFENCE_EVENT
return PendingIntent.getBroadcast(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)

}
}

Expand All @@ -48,8 +48,8 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
Toast.makeText(context, "Geofence update received", Toast.LENGTH_SHORT).show()
Log.e("GFBroadcastReceiver", "geofence update receive")

val tripString = intent.getStringExtra(TripAlarmBroadcastReceiver.EXTRA_START_TRIP_EVENT_TRIP)
val tripGroupUuid = intent.getStringExtra(TripAlarmBroadcastReceiver.EXTRA_START_TRIP_EVENT_TRIP_GROUP_UUID)
val tripString = intent.getStringExtra(EXTRA_TRIP)
val tripGroupUuid = intent.getStringExtra(EXTRA_TRIP_GROUP_UUID)

if (intent.action == ACTION_GEOFENCE_EVENT) {
val geofences: List<com.skedgo.tripkit.routing.Geofence> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.skedgo.tripkit.routing

import com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_ENTER
import com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_EXIT
import com.google.android.gms.location.Geofence.NEVER_EXPIRE

fun Geofence.toGsmGeofence(): com.google.android.gms.location.Geofence {

Expand All @@ -15,13 +16,10 @@ fun Geofence.toGsmGeofence(): com.google.android.gms.location.Geofence {
null
}

val geofenceBuilder = com.google.android.gms.location.Geofence.Builder()
.setRequestId(this.id)
.setCircularRegion(this.center.lat, this.center.lng, this.radius.toFloat())
.setExpirationDuration(timeline)
if (transitionType != null) {
geofenceBuilder.setTransitionTypes(transitionType)
}

return geofenceBuilder.build()
return com.google.android.gms.location.Geofence.Builder()
.setRequestId(this.id)
.setCircularRegion(this.center.lat, this.center.lng, this.radius.toFloat())
.setExpirationDuration(if (this.timeline <= 0) NEVER_EXPIRE else this.timeline)
.setTransitionTypes(transitionType ?: GEOFENCE_TRANSITION_ENTER)
.build()
}

0 comments on commit 95abb30

Please sign in to comment.