From f5aa9da84c3604e337ab5678393db81fc8951f7c Mon Sep 17 00:00:00 2001 From: Fabian Wiesel Date: Fri, 1 Mar 2024 17:24:35 +0100 Subject: [PATCH] Various style fixes --- .../childmonitor/DiscoverActivity.kt | 2 +- .../rochefort/childmonitor/ListenService.kt | 9 +++---- .../rochefort/childmonitor/MonitorService.kt | 25 ++++++++++--------- .../de/rochefort/childmonitor/VolumeView.kt | 8 +++--- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt b/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt index 731d2f8..1f46d62 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/DiscoverActivity.kt @@ -158,7 +158,7 @@ class DiscoverActivity : Activity() { } } } - this@DiscoverActivity.nsdManager!!.resolveService(service, resolver) + this@DiscoverActivity.nsdManager.resolveService(service, resolver) } else { Log.d(TAG, "Unknown Service name: " + service.serviceName) } diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt b/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt index 14e5041..2b9619e 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/ListenService.kt @@ -72,14 +72,11 @@ class ListenService : Service() { } override fun onDestroy() { - this.listenThread?.let { - this.listenThread = null - it.interrupt() - } + this.listenThread?.interrupt() + this.listenThread = null // Cancel the persistent notification. - val NOTIFICATION = R.string.listening - notificationManager.cancel(NOTIFICATION) + notificationManager.cancel(R.string.listening) ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) // Tell the user we stopped. Toast.makeText(this, R.string.stopped, Toast.LENGTH_SHORT).show() diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt b/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt index d29128c..afb4b22 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/MonitorService.kt @@ -166,17 +166,18 @@ class MonitorService : Service() { // Save the service name. Android may have changed it in order to // resolve a conflict, so update the name you initially requested // with the name Android actually used. - val serviceName = nsdServiceInfo.serviceName - Log.i(TAG, "Service name: $serviceName") - val ma = monitorActivity - ma?.runOnUiThread { - val statusText = ma.findViewById(R.id.textStatus) - statusText.setText(R.string.waitingForParent) - val serviceText = ma.findViewById(R.id.textService) - serviceText.text = serviceName - val portText = ma.findViewById(R.id.port) - portText.text = Integer.toString(port) - } + nsdServiceInfo.serviceName.let { serviceName -> + Log.i(TAG, "Service name: $serviceName") + monitorActivity?.let { ma -> + ma.runOnUiThread { + val statusText = ma.findViewById(R.id.textStatus) + statusText.setText(R.string.waitingForParent) + val serviceText = ma.findViewById(R.id.textService) + serviceText.text = serviceName + val portText = ma.findViewById(R.id.port) + portText.text = port.toString() + } + } } override fun onRegistrationFailed(serviceInfo: NsdServiceInfo, errorCode: Int) { @@ -237,13 +238,13 @@ class MonitorService : Service() { unregisterService() this.connectionToken = null this.currentSocket?.let { - this.currentSocket = null try { it.close() } catch (e: IOException) { Log.e(TAG, "Failed to close active socket on port $currentPort") } } + this.currentSocket = null // Cancel the persistent notification. this.notificationManager.cancel(R.string.listening) diff --git a/app/src/main/kotlin/de/rochefort/childmonitor/VolumeView.kt b/app/src/main/kotlin/de/rochefort/childmonitor/VolumeView.kt index 2d23df8..67d6b95 100644 --- a/app/src/main/kotlin/de/rochefort/childmonitor/VolumeView.kt +++ b/app/src/main/kotlin/de/rochefort/childmonitor/VolumeView.kt @@ -22,6 +22,7 @@ import android.graphics.Color import android.graphics.Paint import android.util.AttributeSet import android.view.View +import kotlin.math.min class VolumeView : View { private val paint: Paint @@ -52,8 +53,7 @@ class VolumeView : View { val size = volumeHistory.size() // Size is at most width val volumeNorm = volumeHistory.volumeNorm val relativeBrightness: Double = if (size > 0) { - val normalizedVolume = volumeHistory[size - 1] - Math.max(0.3, normalizedVolume) + volumeHistory[size - 1].coerceAtLeast(0.3) } else { 0.3 } @@ -73,11 +73,11 @@ class VolumeView : View { } val margins = height * 0.1 val graphHeight = height - 2.0 * margins - val leftMost = Math.max(0, volumeHistory.size() - width) + val leftMost = (volumeHistory.size() - width).coerceAtLeast(0) val graphScale = graphHeight * volumeNorm var xPrev = 0 var yPrev = (margins + graphHeight - volumeHistory[leftMost] * graphScale).toInt() - val length = Math.min(size, width) + val length = min(size, width) for (xNext in 1 until length - 1) { val yNext = (margins + graphHeight - volumeHistory[leftMost + xNext] * graphScale).toInt() canvas.drawLine(xPrev.toFloat(), yPrev.toFloat(), xNext.toFloat(), yNext.toFloat(), paint)