Use lateinit for notificationManager and nsdManager

This commit is contained in:
Fabian Wiesel
2024-02-26 21:05:11 +01:00
parent 41506bc601
commit 32235302c6
3 changed files with 12 additions and 12 deletions

View File

@@ -34,11 +34,11 @@ import android.widget.ListView
import android.widget.Toast import android.widget.Toast
class DiscoverActivity : Activity() { class DiscoverActivity : Activity() {
private var nsdManager: NsdManager? = null private lateinit var nsdManager: NsdManager
private var discoveryListener: DiscoveryListener? = null private var discoveryListener: DiscoveryListener? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Log.i(TAG, "ChildMonitor start") Log.i(TAG, "ChildMonitor start")
nsdManager = this.getSystemService(NSD_SERVICE) as NsdManager this.nsdManager = this.getSystemService(NSD_SERVICE) as NsdManager
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_discover) setContentView(R.layout.activity_discover)
val discoverChildButton = findViewById<Button>(R.id.discoverChildButton) val discoverChildButton = findViewById<Button>(R.id.discoverChildButton)
@@ -94,7 +94,7 @@ class DiscoverActivity : Activity() {
this.discoveryListener?.let { this.discoveryListener?.let {
this.discoveryListener = null this.discoveryListener = null
Log.i(TAG, "Unregistering monitoring service") Log.i(TAG, "Unregistering monitoring service")
this.nsdManager!!.stopServiceDiscovery(it) this.nsdManager.stopServiceDiscovery(it)
} }
super.onDestroy() super.onDestroy()
} }

View File

@@ -43,7 +43,7 @@ class ListenService : Service() {
private val bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding) private val bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding)
private val byteBufferSize = bufferSize * 2 private val byteBufferSize = bufferSize * 2
private val binder: IBinder = ListenBinder() private val binder: IBinder = ListenBinder()
private var notificationManager: NotificationManager? = null private lateinit var notificationManager: NotificationManager
private var listenThread: Thread? = null private var listenThread: Thread? = null
val volumeHistory = VolumeHistory(16384) val volumeHistory = VolumeHistory(16384)
var childDeviceName: String? = null var childDeviceName: String? = null
@@ -79,7 +79,7 @@ class ListenService : Service() {
// Cancel the persistent notification. // Cancel the persistent notification.
val NOTIFICATION = R.string.listening val NOTIFICATION = R.string.listening
notificationManager!!.cancel(NOTIFICATION) notificationManager.cancel(NOTIFICATION)
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
// Tell the user we stopped. // Tell the user we stopped.
Toast.makeText(this, R.string.stopped, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.stopped, Toast.LENGTH_SHORT).show()
@@ -115,7 +115,7 @@ class ListenService : Service() {
"Foreground Service Channel", "Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT NotificationManager.IMPORTANCE_DEFAULT
) )
notificationManager!!.createNotificationChannel(serviceChannel) notificationManager.createNotificationChannel(serviceChannel)
} }
} }

View File

@@ -41,12 +41,12 @@ import java.net.Socket
class MonitorService : Service() { class MonitorService : Service() {
private val binder: IBinder = MonitorBinder() private val binder: IBinder = MonitorBinder()
private var nsdManager: NsdManager? = null private lateinit var nsdManager: NsdManager
private var registrationListener: RegistrationListener? = null private var registrationListener: RegistrationListener? = null
private var currentSocket: ServerSocket? = null private var currentSocket: ServerSocket? = null
private var connectionToken: Any? = null private var connectionToken: Any? = null
private var currentPort = 0 private var currentPort = 0
private var notificationManager: NotificationManager? = null private lateinit var notificationManager: NotificationManager
private var monitorThread: Thread? = null private var monitorThread: Thread? = null
private var monitorActivity: MonitorActivity? = null private var monitorActivity: MonitorActivity? = null
fun setMonitorActivity(monitorActivity: MonitorActivity?) { fun setMonitorActivity(monitorActivity: MonitorActivity?) {
@@ -195,7 +195,7 @@ class MonitorService : Service() {
Log.e(TAG, "Unregistration failed: $errorCode") Log.e(TAG, "Unregistration failed: $errorCode")
} }
} }
nsdManager!!.registerService( nsdManager.registerService(
serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener) serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener)
} }
@@ -203,7 +203,7 @@ class MonitorService : Service() {
this.registrationListener?.let { this.registrationListener?.let {
this.registrationListener = null this.registrationListener = null
Log.i(TAG, "Unregistering monitoring service") Log.i(TAG, "Unregistering monitoring service")
this.nsdManager!!.unregisterService(it) this.nsdManager.unregisterService(it)
} }
} }
@@ -214,7 +214,7 @@ class MonitorService : Service() {
"Foreground Service Channel", "Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT NotificationManager.IMPORTANCE_DEFAULT
) )
this.notificationManager!!.createNotificationChannel(serviceChannel) this.notificationManager.createNotificationChannel(serviceChannel)
} }
} }
@@ -246,7 +246,7 @@ class MonitorService : Service() {
} }
// Cancel the persistent notification. // Cancel the persistent notification.
this.notificationManager!!.cancel(R.string.listening) this.notificationManager.cancel(R.string.listening)
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
// Tell the user we stopped. // Tell the user we stopped.
Toast.makeText(this, R.string.stopped, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.stopped, Toast.LENGTH_SHORT).show()