Use ?.let instead if-null-test, avoid runnable

This commit is contained in:
Fabian Wiesel
2024-02-26 19:40:57 +01:00
parent fa6e4da2c5
commit 58095bc67b
5 changed files with 44 additions and 55 deletions

View File

@@ -91,10 +91,10 @@ class DiscoverActivity : Activity() {
override fun onDestroy() {
Log.i(TAG, "ChildMonitoring stop")
if (this.discoveryListener != null) {
Log.i(TAG, "Unregistering monitoring service")
this.nsdManager!!.stopServiceDiscovery(discoveryListener)
this.discoveryListener?.let {
this.discoveryListener = null
Log.i(TAG, "Unregistering monitoring service")
this.nsdManager!!.stopServiceDiscovery(it)
}
super.onDestroy()
}
@@ -102,17 +102,14 @@ class DiscoverActivity : Activity() {
private fun startServiceDiscovery(serviceType: String) {
val nsdManager = this.getSystemService(NSD_SERVICE) as NsdManager
val wifi = this.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
val multicastReleaser: Runnable
multicastReleaser = run {
val multicastLock = wifi.createMulticastLock("multicastLock")
multicastLock.setReferenceCounted(true)
multicastLock.acquire()
Runnable {
try {
multicastLock.release()
} catch (ignored: Exception) {
//dont really care
}
val multicastLock = wifi.createMulticastLock("multicastLock")
multicastLock.setReferenceCounted(true)
multicastLock.acquire()
val multicastReleaser = {
try {
multicastLock.release()
} catch (ignored: Exception) {
// don't really care
}
}
val serviceTable = findViewById<ListView>(R.id.ServiceTable)
@@ -121,9 +118,8 @@ class DiscoverActivity : Activity() {
serviceTable.adapter = availableServicesAdapter
serviceTable.onItemClickListener = OnItemClickListener { parent: AdapterView<*>, _: View?, position: Int, _: Long ->
val info = parent.getItemAtPosition(position) as ServiceInfoWrapper
val address = info.address
if (address != null) {
connectToChild(address, info.port, info.name)
info.address?.let {
connectToChild(it, info.port, info.name)
}
}
@@ -172,24 +168,24 @@ class DiscoverActivity : Activity() {
// When the network service is no longer available.
// Internal bookkeeping code goes here.
Log.e(TAG, "Service lost: $service")
multicastReleaser.run()
multicastReleaser()
}
override fun onDiscoveryStopped(serviceType: String) {
Log.i(TAG, "Discovery stopped: $serviceType")
multicastReleaser.run()
multicastReleaser()
}
override fun onStartDiscoveryFailed(serviceType: String, errorCode: Int) {
Log.e(TAG, "Discovery failed: Error code: $errorCode")
nsdManager.stopServiceDiscovery(this)
multicastReleaser.run()
multicastReleaser()
}
override fun onStopDiscoveryFailed(serviceType: String, errorCode: Int) {
Log.e(TAG, "Discovery failed: Error code: $errorCode")
nsdManager.stopServiceDiscovery(this)
multicastReleaser.run()
multicastReleaser()
}
}
nsdManager.discoverServices(

View File

@@ -65,10 +65,10 @@ class ListenActivity : Activity() {
private fun ensureServiceRunningAndBind(bundle: Bundle?) {
val context: Context = this
val intent = Intent(context, ListenService::class.java)
if (bundle != null) {
intent.putExtra("name", bundle.getString("name"))
intent.putExtra("address", bundle.getString("address"))
intent.putExtra("port", bundle.getInt("port"))
bundle?.let {
intent.putExtra("name", it.getString("name"))
intent.putExtra("address", it.getString("address"))
intent.putExtra("port", it.getInt("port"))
ContextCompat.startForegroundService(context, intent)
}
// Attempts to establish a connection with the service. We use an

View File

@@ -58,25 +58,23 @@ class ListenService : Service() {
Log.i(TAG, "Received start id $startId: $intent")
// Display a notification about us starting. We put an icon in the status bar.
createNotificationChannel()
val extras = intent.extras
if (extras != null) {
val name = extras.getString("name")
intent.extras?.let {
val name = it.getString("name")
childDeviceName = name
val n = buildNotification(name)
val foregroundServiceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK else 0 // Keep the linter happy
ServiceCompat.startForeground(this, ID, n, foregroundServiceType)
val address = extras.getString("address")
val port = extras.getInt("port")
val address = it.getString("address")
val port = it.getInt("port")
doListen(address, port)
}
return START_REDELIVER_INTENT
}
override fun onDestroy() {
val lt = this.listenThread
if (lt != null) {
lt.interrupt()
this.listenThread?.let {
this.listenThread = null
it.interrupt()
}
// Cancel the persistent notification.
@@ -121,12 +119,12 @@ class ListenService : Service() {
}
}
fun setErrorCallback(errorCallback: Runnable?) {
mErrorCallback = errorCallback
fun setErrorCallback(errorCallback: (() -> Unit)) {
this.errorCallback = errorCallback
}
fun setUpdateCallback(updateCallback: Runnable?) {
mUpdateCallback = updateCallback
fun setUpdateCallback(updateCallback: (() -> Unit)) {
this.updateCallback = updateCallback
}
inner class ListenBinder : Binder() {
@@ -134,8 +132,8 @@ class ListenService : Service() {
get() = this@ListenService
}
private var mErrorCallback: Runnable? = null
private var mUpdateCallback: Runnable? = null
private var errorCallback: (() -> Unit)? = null
private var updateCallback: (() -> Unit)? = null
private fun doListen(address: String?, port: Int) {
val lt = Thread {
try {
@@ -150,8 +148,7 @@ class ListenService : Service() {
// an alert to notify the user that the connection has been
// interrupted.
playAlert()
val errorCallback = mErrorCallback
errorCallback?.run()
errorCallback?.invoke()
}
}
this.listenThread = lt
@@ -181,8 +178,7 @@ class ListenService : Service() {
val decodedBytes = ShortArray(decoded)
System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded)
volumeHistory.onAudioData(decodedBytes)
val updateCallback = mUpdateCallback
updateCallback?.run()
updateCallback?.invoke()
}
}
} catch (e: Exception) {

View File

@@ -120,9 +120,9 @@ class MonitorActivity : Activity() {
private fun doUnbindAndStopService() {
if (this.shouldUnbind) {
this.shouldUnbind = false
// Release information about the service's state.
unbindService(connection)
this.shouldUnbind = false
}
val context: Context = this
val intent = Intent(context, MonitorService::class.java)

View File

@@ -200,11 +200,10 @@ class MonitorService : Service() {
}
private fun unregisterService() {
val currentListener = this.registrationListener
if (currentListener != null) {
Log.i(TAG, "Unregistering monitoring service")
this.nsdManager!!.unregisterService(currentListener)
this.registrationListener?.let {
this.registrationListener = null
Log.i(TAG, "Unregistering monitoring service")
this.nsdManager!!.unregisterService(it)
}
}
@@ -231,18 +230,16 @@ class MonitorService : Service() {
}
override fun onDestroy() {
val mt = this.monitorThread
if (mt != null) {
mt.interrupt()
this.monitorThread?.let {
this.monitorThread = null
it.interrupt()
}
unregisterService()
this.connectionToken = null
val sock = this.currentSocket
if (sock != null) {
this.currentSocket?.let {
this.currentSocket = null
try {
sock.close()
this.currentSocket = null
it.close()
} catch (e: IOException) {
Log.e(TAG, "Failed to close active socket on port $currentPort")
}