Skip to content

Commit

Permalink
fix(geofence): use coreSdkThread on triggered geofences, init nearest…
Browse files Browse the repository at this point in the history
…Geofences

SUITEDEV-26055

Co-authored-by: LasOri <[email protected]>
Co-authored-by: davidSchuppa <[email protected]>
  • Loading branch information
3 people committed Jan 5, 2021
1 parent b44611b commit 28ff4b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import android.location.LocationManager
import android.os.Build
import com.emarsys.core.api.MissingPermissionException
import com.emarsys.core.api.result.CompletionListener
import com.emarsys.core.concurrency.CoreSdkHandler
import com.emarsys.core.concurrency.CoreSdkHandlerProvider
import com.emarsys.core.di.DependencyInjection
import com.emarsys.core.permission.PermissionChecker
import com.emarsys.core.request.RequestManager
import com.emarsys.core.request.model.RequestModel
import com.emarsys.core.response.ResponseModel
import com.emarsys.core.storage.Storage
import com.emarsys.mobileengage.api.event.EventHandler
import com.emarsys.mobileengage.event.EventHandlerProvider
import com.emarsys.mobileengage.fake.FakeMobileEngageDependencyContainer
import com.emarsys.mobileengage.fake.FakeRequestManager
import com.emarsys.mobileengage.geofence.model.*
import com.emarsys.mobileengage.notification.ActionCommandFactory
Expand Down Expand Up @@ -97,6 +101,8 @@ class DefaultGeofenceInternalTest {
mockLocation = mock()
mockGeofencingClient = mock()

DependencyInjection.setup(FakeMobileEngageDependencyContainer())

mockContext = mock()
mockActionCommandFactory = mock()
mockEventHandlerProvider = mock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.emarsys.core.CoreCompletionHandler
import com.emarsys.core.Mockable
import com.emarsys.core.api.MissingPermissionException
import com.emarsys.core.api.result.CompletionListener
import com.emarsys.core.di.getDependency
import com.emarsys.core.permission.PermissionChecker
import com.emarsys.core.request.RequestManager
import com.emarsys.core.response.ResponseModel
Expand Down Expand Up @@ -55,9 +56,9 @@ class DefaultGeofenceInternal(private val requestModelFactory: MobileEngageReque
const val MAX_WAIT_TIME: Long = 60_000
}

private val geofenceBroadcastReceiver = GeofenceBroadcastReceiver()
private val geofenceBroadcastReceiver = GeofenceBroadcastReceiver(getDependency("coreSdkHandler"))
private var geofenceResponse: GeofenceResponse? = null
private lateinit var nearestGeofences: MutableList<MEGeofence>
private var nearestGeofences: MutableList<MEGeofence> = mutableListOf()
private var currentLocation: Location? = null
private val geofencePendingIntent: PendingIntent by lazy {
geofencePendingIntentProvider.providePendingIntent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.emarsys.mobileengage.geofence
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Handler
import com.emarsys.core.Mockable
import com.emarsys.core.concurrency.CoreSdkHandler
import com.emarsys.core.di.getDependency
import com.emarsys.core.util.SystemUtils
import com.emarsys.core.util.log.Logger
Expand All @@ -13,22 +15,24 @@ import com.emarsys.mobileengage.geofence.model.TriggeringGeofence
import com.google.android.gms.location.GeofencingEvent

@Mockable
class GeofenceBroadcastReceiver : BroadcastReceiver() {
class GeofenceBroadcastReceiver(val coreSdkHandler: Handler) : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)
val geofenceInternal = getDependency<GeofenceInternal>("defaultInstance")
if (geofencingEvent.triggeringGeofences != null) {
geofenceInternal.onGeofenceTriggered(geofencingEvent.triggeringGeofences.map {
TriggeringGeofence(it.requestId, convertTransitionToTriggerType(geofencingEvent.geofenceTransition))
})
geofencingEvent.triggeringGeofences.forEach {
val status = mapOf(
"triggerType" to convertTransitionToTriggerType(geofencingEvent.geofenceTransition),
"geofenceId" to it.requestId
)
Logger.debug(StatusLog(GeofenceBroadcastReceiver::class.java, SystemUtils.getCallerMethodName(), mapOf(), status
))
coreSdkHandler.post {
val geofenceInternal = getDependency<GeofenceInternal>("defaultInstance")
if (geofencingEvent.triggeringGeofences != null) {
geofenceInternal.onGeofenceTriggered(geofencingEvent.triggeringGeofences.map {
TriggeringGeofence(it.requestId, convertTransitionToTriggerType(geofencingEvent.geofenceTransition))
})
geofencingEvent.triggeringGeofences.forEach {
val status = mapOf(
"triggerType" to convertTransitionToTriggerType(geofencingEvent.geofenceTransition),
"geofenceId" to it.requestId
)
Logger.debug(StatusLog(GeofenceBroadcastReceiver::class.java, SystemUtils.getCallerMethodName(), mapOf(), status
))
}
}
}
}
Expand Down

0 comments on commit 28ff4b1

Please sign in to comment.