Merge pull request #14 from fwiesel/less_setters

Remove setters, and rename callbacks for onSomething
This commit is contained in:
edr
2024-04-03 17:18:18 +02:00
committed by GitHub
4 changed files with 8 additions and 19 deletions

View File

@@ -48,8 +48,8 @@ class ListenActivity : Activity() {
connectedText.text = bs.childDeviceName connectedText.text = bs.childDeviceName
val volumeView = findViewById<VolumeView>(R.id.volume) val volumeView = findViewById<VolumeView>(R.id.volume)
volumeView.volumeHistory = bs.volumeHistory volumeView.volumeHistory = bs.volumeHistory
bs.setUpdateCallback { volumeView.postInvalidate() } bs.onUpdate = { volumeView.postInvalidate() }
bs.setErrorCallback { postErrorMessage() } bs.onError = { postErrorMessage() }
} }
override fun onServiceDisconnected(className: ComponentName) { override fun onServiceDisconnected(className: ComponentName) {

View File

@@ -116,21 +116,13 @@ class ListenService : Service() {
} }
} }
fun setErrorCallback(errorCallback: (() -> Unit)) {
this.errorCallback = errorCallback
}
fun setUpdateCallback(updateCallback: (() -> Unit)) {
this.updateCallback = updateCallback
}
inner class ListenBinder : Binder() { inner class ListenBinder : Binder() {
val service: ListenService val service: ListenService
get() = this@ListenService get() = this@ListenService
} }
private var errorCallback: (() -> Unit)? = null var onError: (() -> Unit)? = null
private var updateCallback: (() -> Unit)? = null var onUpdate: (() -> Unit)? = null
private fun doListen(address: String?, port: Int) { private fun doListen(address: String?, port: Int) {
val lt = Thread { val lt = Thread {
try { try {
@@ -139,7 +131,7 @@ class ListenService : Service() {
val success = streamAudio(socket) val success = streamAudio(socket)
if (!success) { if (!success) {
playAlert() playAlert()
errorCallback?.invoke() onError?.invoke()
} }
} catch (e : IOException) { } catch (e : IOException) {
Log.e(TAG, "Error opening socket to $address on port $port", e) Log.e(TAG, "Error opening socket to $address on port $port", e)
@@ -186,7 +178,7 @@ class ListenService : Service() {
val decodedBytes = ShortArray(decoded) val decodedBytes = ShortArray(decoded)
System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded) System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded)
volumeHistory.onAudioData(decodedBytes) volumeHistory.onAudioData(decodedBytes)
updateCallback?.invoke() onUpdate?.invoke()
} }
} }
return true return true

View File

@@ -39,7 +39,7 @@ class MonitorActivity : Activity() {
// service that we know is running in our own process, we can // service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it. // cast its IBinder to a concrete class and directly access it.
val bs = (service as MonitorBinder).service val bs = (service as MonitorBinder).service
bs.setMonitorActivity(this@MonitorActivity) bs.monitorActivity = this@MonitorActivity
} }
override fun onServiceDisconnected(className: ComponentName) { override fun onServiceDisconnected(className: ComponentName) {

View File

@@ -48,10 +48,7 @@ class MonitorService : Service() {
private var currentPort = 0 private var currentPort = 0
private lateinit var notificationManager: NotificationManager private lateinit var notificationManager: NotificationManager
private var monitorThread: Thread? = null private var monitorThread: Thread? = null
private var monitorActivity: MonitorActivity? = null var monitorActivity: MonitorActivity? = null
fun setMonitorActivity(monitorActivity: MonitorActivity?) {
this.monitorActivity = monitorActivity
}
private fun serviceConnection(socket: Socket) { private fun serviceConnection(socket: Socket) {
val ma = this.monitorActivity val ma = this.monitorActivity