From 58095bc67b0a97d6048a38f57bf7b2c005b26e2a Mon Sep 17 00:00:00 2001 From: Fabian Wiesel Date: Mon, 26 Feb 2024 19:40:57 +0100 Subject: [PATCH] Use ?.let instead if-null-test, avoid runnable --- .../childmonitor/DiscoverActivity.kt | 38 +++++++++---------- .../rochefort/childmonitor/ListenActivity.kt | 8 ++-- .../rochefort/childmonitor/ListenService.kt | 32 +++++++--------- .../rochefort/childmonitor/MonitorActivity.kt | 2 +- .../rochefort/childmonitor/MonitorService.kt | 19 ++++------ 5 files changed, 44 insertions(+), 55 deletions(-) diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt b/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt index 8891c23..0fc5724 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt @@ -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(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( diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/ListenActivity.kt b/app/src/main/kotlin/de/rochefort/childmonitor/ListenActivity.kt index 43b7051..14501d6 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/ListenActivity.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/ListenActivity.kt @@ -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 diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt b/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt index 8204c9d..8e0fe33 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt @@ -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) { diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/MonitorActivity.kt b/app/src/main/kotlin/de/rochefort/childmonitor/MonitorActivity.kt index 5edc4b6..3047f54 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/MonitorActivity.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/MonitorActivity.kt @@ -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) diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt b/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt index 8dd980f..3e5c739 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt @@ -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") }