Skip to content

Commit

Permalink
SourceStatus update will now trigger radarservice without using broad…
Browse files Browse the repository at this point in the history
…caster
  • Loading branch information
this-Aditya committed Nov 5, 2024
1 parent 263a058 commit 9dd931d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ class GooglePlacesService: SourceService<GooglePlacesState>() {
val currentManager = sourceManager
(currentManager as? GooglePlacesManager)?.reScan()
if (currentManager == null) {
broadcaster?.send(SOURCE_STATUS_CHANGED) {
putExtra(SOURCE_STATUS_CHANGED, SourceStatusListener.Status.DISCONNECTED.ordinal)
putExtra(SOURCE_SERVICE_CLASS, this@GooglePlacesService.javaClass.name)
}
super.status.value = SourceStatusListener.Status.DISCONNECTED
}
stopRecording()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.radarbase.android
import android.Manifest.permission.*
import android.app.Notification
import android.app.PendingIntent
import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
Expand Down Expand Up @@ -488,12 +489,12 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis
startScanning()
}
if (showSourceStatus) {
mainHandler.post {
lifecycleScope.launch(Dispatchers.Main.immediate) {
val showRes = when (status) {
SourceStatusListener.Status.READY -> R.string.device_ready
SourceStatusListener.Status.CONNECTED -> R.string.device_connected
SourceStatusListener.Status.CONNECTING -> R.string.device_connecting
SourceStatusListener.Status.DISCONNECTING -> return@post // do not show toast
SourceStatusListener.Status.DISCONNECTING -> return@launch // do not show toast
SourceStatusListener.Status.DISCONNECTED -> R.string.device_disconnected
SourceStatusListener.Status.UNAVAILABLE -> R.string.device_unavailable
}
Expand Down Expand Up @@ -672,15 +673,7 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis
failedSourceObserverJob = lifecycleScope.launch {
mConnections.forEach {
it.connection.sourceConnectFailed
?.onEach { reason: SourceService.SourceConnectFailed ->
ensureActive()
if (isScanningEnabled) {
Boast.makeText(this@RadarService,
getString(R.string.cannot_connect_device,
reason.sourceName),
Toast.LENGTH_SHORT).show()
}
}?.launchIn(this)
?.onEach { }?.launchIn(this)
}
}

Expand All @@ -689,6 +682,15 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis
}
}

fun sourceFailedToConnect(sourceName: String, serviceClass: Class<in SourceService<*>>) {
logger.info("Source {} of service class {} failed to connect", sourceName, serviceClass)
if (isScanningEnabled) {
Boast.makeText(this@RadarService,
getString(R.string.cannot_connect_device, sourceName),
Toast.LENGTH_SHORT).show()
}
}

override fun loginFailed(manager: LoginManager?, ex: Exception?) {
isMakingAuthRequest.set(false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.os.IBinder
import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import org.radarbase.android.auth.SourceMetadata
import org.radarbase.android.kafka.ServerStatus
import org.radarbase.android.kafka.TopicSendResult
Expand All @@ -32,7 +33,7 @@ import org.radarbase.util.Strings
import org.slf4j.LoggerFactory
import java.io.IOException

open class BaseServiceConnection<S : BaseSourceState>(private val serviceClassName: String) : ServiceConnection {
open class BaseServiceConnection<S : BaseSourceState>(protected val serviceClassName: String) : ServiceConnection {
@get:Synchronized
protected var serviceBinder: SourceBinder<S>? = null
private set
Expand Down Expand Up @@ -62,7 +63,7 @@ open class BaseServiceConnection<S : BaseSourceState>(private val serviceClassNa
serviceBinder?.manualAttributes = value
}

val sourceStatus: LiveData<SourceStatusListener.Status>?
val sourceStatus: StateFlow<SourceStatusListener.Status>?
get() = serviceBinder?.sourceStatus

val sourceConnectFailed: SharedFlow<SourceService.SourceConnectFailed>?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.os.Bundle
import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import org.radarbase.android.auth.SourceMetadata
import org.radarbase.android.kafka.ServerStatus
import org.radarbase.android.kafka.TopicSendResult
Expand Down Expand Up @@ -66,6 +67,6 @@ interface SourceBinder<T : BaseSourceState> {
fun needsBluetooth(): Boolean

fun shouldRemainInBackground(): Boolean
val sourceStatus: LiveData<SourceStatusListener.Status>
val sourceStatus: StateFlow<SourceStatusListener.Status>
val sourceConnectFailed: SharedFlow<SourceConnectFailed>
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.lifecycle.LifecycleService
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -76,9 +77,9 @@ abstract class SourceService<T : BaseSourceState> :
var sourceTypes: Set<SourceType> = emptySet()
var sourceConnectFailed: MutableSharedFlow<SourceConnectFailed> = MutableSharedFlow()

val status: MutableLiveData<SourceStatusListener.Status> by lazy {
MutableLiveData(SourceStatusListener.Status.DISCONNECTED)
}
val status: MutableStateFlow<SourceStatusListener.Status> =
MutableStateFlow(SourceStatusListener.Status.DISCONNECTED)


private val acceptableSourceTypes: Set<SourceType>
get() = sourceTypes.filterTo(HashSet()) {
Expand Down Expand Up @@ -288,7 +289,7 @@ abstract class SourceService<T : BaseSourceState> :
}

private fun broadcastSourceStatus(status: SourceStatusListener.Status) {
this.status.postValue(status)
this.status.value = status
}

override fun sourceStatusUpdated(manager: SourceManager<*>, status: SourceStatusListener.Status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.io.IOException
import java.util.*

class SourceServiceBinder<T : BaseSourceState>(private val sourceService: SourceService<T>) : Binder(), SourceBinder<T> {
override val sourceStatus: LiveData<SourceStatusListener.Status>
override val sourceStatus: StateFlow<SourceStatusListener.Status>
get() = sourceService.status

override val sourceConnectFailed: SharedFlow<SourceService.SourceConnectFailed>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ package org.radarbase.android.source
import android.content.ComponentName
import android.content.Context
import android.os.IBinder
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.radarbase.android.RadarService
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class SourceServiceConnection<S : BaseSourceState>(
private val radarService: RadarService,
Expand All @@ -28,7 +33,25 @@ class SourceServiceConnection<S : BaseSourceState>(
val context: Context
get() = radarService

private var serviceJob: Job? = null
private var sourceFailedJob: Job? = null

override fun onServiceConnected(className: ComponentName?, service: IBinder?) {
radarService.run {
serviceJob = lifecycleScope.launch {
sourceStatus
?.collect {
logger.info("Source status changed of service: $serviceClassName")
radarService.sourceStatusUpdated(this@SourceServiceConnection, it)
}
}
sourceFailedJob = lifecycleScope.launch {
sourceConnectFailed?.collect {
radarService.sourceFailedToConnect(it.sourceName, it.serviceClass)
}
}
}

if (!hasService()) {
super.onServiceConnected(className, service)
if (hasService()) {
Expand All @@ -38,11 +61,16 @@ class SourceServiceConnection<S : BaseSourceState>(
}

override fun onServiceDisconnected(className: ComponentName?) {
serviceJob?.cancel()
sourceFailedJob?.cancel()
val hadService = hasService()
super.onServiceDisconnected(className)

if (hadService) {
radarService.serviceDisconnected(this)
}
}
companion object {
private val logger: Logger = LoggerFactory.getLogger(SourceServiceConnection::class.java)
}
}

0 comments on commit 9dd931d

Please sign in to comment.