diff --git a/CommonCoreLegacy/src/main/java/com/skedgo/tripkit/routing/Geofence.kt b/CommonCoreLegacy/src/main/java/com/skedgo/tripkit/routing/Geofence.kt index 420debe6..d5f60863 100644 --- a/CommonCoreLegacy/src/main/java/com/skedgo/tripkit/routing/Geofence.kt +++ b/CommonCoreLegacy/src/main/java/com/skedgo/tripkit/routing/Geofence.kt @@ -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 } } diff --git a/TripKitAndroid/src/main/java/com/skedgo/tripkit/Configs.java b/TripKitAndroid/src/main/java/com/skedgo/tripkit/Configs.java index 2c226e02..45467ad5 100644 --- a/TripKitAndroid/src/main/java/com/skedgo/tripkit/Configs.java +++ b/TripKitAndroid/src/main/java/com/skedgo/tripkit/Configs.java @@ -71,4 +71,6 @@ public interface Configs { @Value.Default public default boolean hideFavorites() { return false;} + @Value.Default public default boolean showGeofences() { return false;} + } diff --git a/TripKitAndroid/src/main/java/com/skedgo/tripkit/LocationUtils.kt b/TripKitAndroid/src/main/java/com/skedgo/tripkit/LocationUtils.kt new file mode 100644 index 00000000..cbbc139e --- /dev/null +++ b/TripKitAndroid/src/main/java/com/skedgo/tripkit/LocationUtils.kt @@ -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) +} \ No newline at end of file diff --git a/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeoLocation.kt b/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeoLocation.kt index d6f1fe62..a02a24ab 100644 --- a/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeoLocation.kt +++ b/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeoLocation.kt @@ -22,37 +22,45 @@ object GeoLocation { } private val currentGeofenceList = mutableListOf() 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) { + fun createGeoFences( + trip: Trip, + geofences: List, + addGeofenceListener: (Boolean) -> Unit + ) { val gsmGeofences = mutableListOf() - 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) @@ -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) diff --git a/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceBroadcastReceiver.kt b/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceBroadcastReceiver.kt index c42d4aae..6dc357e0 100644 --- a/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceBroadcastReceiver.kt +++ b/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceBroadcastReceiver.kt @@ -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 @@ -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? = 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 ) - } } @@ -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 = diff --git a/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceUtils.kt b/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceUtils.kt index a9506cca..d8a7956b 100644 --- a/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceUtils.kt +++ b/TripKitAndroid/src/main/java/com/skedgo/tripkit/routing/GeofenceUtils.kt @@ -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 { @@ -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() } \ No newline at end of file