Skip to content

Commit

Permalink
Logger statements to debug coro start of sensors issue
Browse files Browse the repository at this point in the history
  • Loading branch information
itissid committed Nov 5, 2024
1 parent 9a6b544 commit 8c946c8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/me/itissid/privyloci/SensorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import me.itissid.privyloci.datamodels.SensorType
import me.itissid.privyloci.sensors.GoogleFusedLocationSensor
import me.itissid.privyloci.sensors.ISensor
import me.itissid.privyloci.service.PrivyForegroundService
import me.itissid.privyloci.util.Logger
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -56,6 +57,12 @@ class SensorManager @Inject constructor(
}

fun updateActiveSensors(requiredSensors: Set<SensorType>) {
this::class.qualifiedName?.let {
Logger.d(
it,
"Updating active sensors to: $requiredSensors"
)
}
val sensorsToStart = requiredSensors - activeSensors
val sensorsToStop = activeSensors - requiredSensors

Expand All @@ -67,10 +74,12 @@ class SensorManager @Inject constructor(
}

private fun startSensors(sensorType: SensorType) {
this::class.qualifiedName?.let { Logger.d(it, "Starting sensor: $sensorType") }
sensorFor(sensorType).start()
}

private fun stopSensors(sensorType: SensorType) {
this::class.qualifiedName?.let { Logger.d(it, "Stopping sensor: $sensorType") }
sensorFor(sensorType).stop()
}

Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/me/itissid/privyloci/SubscriptionManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,27 @@ class SubscriptionManager @Inject constructor(

// Start processors for new subscriptions
activeSubscriptions.addAll(subscriptions)
this::class.simpleName?.let {
Logger.d(
it,
"Active subscriptions processed by SubscriptionManager: ${activeSubscriptions.size}"
)
}
subscriptions.forEach { subscription ->
val processor = createEventProcessor(subscription, context)
processor.startProcessing()
eventProcessors[subscription.subscriptionId] = processor
}
this::class.simpleName?.let {
Logger.d(
it,
"calling manageSensors for ${activeSubscriptions.size} subscriptions"
)
}
manageSensors()
}
}
// N2S: Leaving the sensor manager start code here for now. Not sure if this is the right place for it.
manageSensors()
}

private fun createEventProcessor(subscription: Subscription, context: Context): EventProcessor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.itissid.privyloci.eventprocessors
import android.app.NotificationManager
import android.content.Context
import android.location.Location
import android.util.Log
import androidx.core.app.NotificationCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -16,7 +17,11 @@ import me.itissid.privyloci.datamodels.LatLng
import me.itissid.privyloci.datamodels.Subscription
import me.itissid.privyloci.datamodels.requiredSensors
import me.itissid.privyloci.sensors.GoogleFusedLocationSensor
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import kotlin.random.Random
import kotlin.time.Duration.Companion.milliseconds

class GeofenceEventProcessor(
private val subscription: Subscription,
Expand All @@ -37,6 +42,10 @@ class GeofenceEventProcessor(
private var lastStateChangeTime = System.currentTimeMillis()

override fun startProcessing() {
Log.d(
"GeofenceEventProcessor",
"Starting geofence event processor for Subscription ID: ${subscription.subscriptionId}"
)
assert(
subscription.requiredSensors().size == 1
) { "Geofence event processor requires exactly one sensor" }
Expand All @@ -51,10 +60,23 @@ class GeofenceEventProcessor(
}

override fun stopProcessing() {
Log.d(
"GeofenceEventProcessor",
"Stopping geofence event processor for Subscription ID: ${subscription.subscriptionId}"
)
job?.cancel()
}

private suspend fun processLocation(location: Location) {
private fun processLocation(location: Location) {
Log.d(
"GeofenceEventProcessor",
"Processing location ${
LocalDateTime.ofInstant(
Instant.ofEpochMilli(location.time),
ZoneId.of("UTC")
)
}"
)
val distance = FloatArray(1)
Location.distanceBetween(
location.latitude, location.longitude,
Expand Down

0 comments on commit 8c946c8

Please sign in to comment.