Unused arguments and unnessecary null checks

This commit is contained in:
Fabian Wiesel
2024-02-25 22:10:26 +01:00
parent 5d17453c14
commit c5b542a333
3 changed files with 21 additions and 23 deletions

View File

@@ -44,9 +44,9 @@ class DiscoverActivity : Activity() {
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)
discoverChildButton.setOnClickListener { v: View? -> loadDiscoveryViaMdns() } discoverChildButton.setOnClickListener { _: View? -> loadDiscoveryViaMdns() }
val enterChildAddressButton = findViewById<Button>(R.id.enterChildAddressButton) val enterChildAddressButton = findViewById<Button>(R.id.enterChildAddressButton)
enterChildAddressButton.setOnClickListener { v: View? -> loadDiscoveryViaAddress() } enterChildAddressButton.setOnClickListener { _: View? -> loadDiscoveryViaAddress() }
} }
private fun loadDiscoveryViaMdns() { private fun loadDiscoveryViaMdns() {
@@ -69,7 +69,7 @@ class DiscoverActivity : Activity() {
} else { } else {
portField.setText("10000") portField.setText("10000")
} }
connectButton.setOnClickListener { v: View? -> connectButton.setOnClickListener { _: View? ->
Log.i(TAG, "Connecting to child device via address") Log.i(TAG, "Connecting to child device via address")
val addressString = addressField.text.toString() val addressString = addressField.text.toString()
val portString = portField.text.toString() val portString = portField.text.toString()
@@ -93,23 +93,19 @@ class DiscoverActivity : Activity() {
override fun onDestroy() { override fun onDestroy() {
Log.i(TAG, "ChildMonitoring stop") Log.i(TAG, "ChildMonitoring stop")
if (discoveryListener != null) { if (this.discoveryListener != null) {
Log.i(TAG, "Unregistering monitoring service") Log.i(TAG, "Unregistering monitoring service")
nsdManager!!.stopServiceDiscovery(discoveryListener) this.nsdManager!!.stopServiceDiscovery(discoveryListener)
discoveryListener = null this.discoveryListener = null
} }
super.onDestroy() super.onDestroy()
} }
fun startServiceDiscovery(serviceType: String) { private fun startServiceDiscovery(serviceType: String) {
val nsdManager = this.getSystemService(NSD_SERVICE) as NsdManager val nsdManager = this.getSystemService(NSD_SERVICE) as NsdManager
if (nsdManager == null) {
Log.e(TAG, "Could not obtain nsdManager")
return
}
val wifi = this.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager val wifi = this.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
val multicastReleaser: Runnable val multicastReleaser: Runnable
multicastReleaser = if (wifi != null) { multicastReleaser = run {
val multicastLock = wifi.createMulticastLock("multicastLock") val multicastLock = wifi.createMulticastLock("multicastLock")
multicastLock.setReferenceCounted(true) multicastLock.setReferenceCounted(true)
multicastLock.acquire() multicastLock.acquire()
@@ -120,20 +116,21 @@ class DiscoverActivity : Activity() {
//dont really care //dont really care
} }
} }
} else {
Runnable {}
} }
val serviceTable = findViewById<ListView>(R.id.ServiceTable) val serviceTable = findViewById<ListView>(R.id.ServiceTable)
val availableServicesAdapter = ArrayAdapter<ServiceInfoWrapper>(this, val availableServicesAdapter = ArrayAdapter<ServiceInfoWrapper>(this,
R.layout.available_children_list) R.layout.available_children_list)
serviceTable.adapter = availableServicesAdapter serviceTable.adapter = availableServicesAdapter
serviceTable.onItemClickListener = OnItemClickListener { parent: AdapterView<*>, view: View?, position: Int, id: Long -> serviceTable.onItemClickListener = OnItemClickListener { parent: AdapterView<*>, _: View?, position: Int, _: Long ->
val info = parent.getItemAtPosition(position) as ServiceInfoWrapper val info = parent.getItemAtPosition(position) as ServiceInfoWrapper
connectToChild(info.address, info.port, info.name) val address = info.address
if (address != null) {
connectToChild(address, info.port, info.name)
}
} }
// Instantiate a new DiscoveryListener // Instantiate a new DiscoveryListener
discoveryListener = object : DiscoveryListener { this.discoveryListener = object : DiscoveryListener {
// Called as soon as service discovery begins. // Called as soon as service discovery begins.
override fun onDiscoveryStarted(regType: String) { override fun onDiscoveryStarted(regType: String) {
Log.d(TAG, "Service discovery started") Log.d(TAG, "Service discovery started")
@@ -223,7 +220,7 @@ internal class ServiceInfoWrapper(private val info: NsdServiceInfo) {
return info.host == other.host && info.port == other.port return info.host == other.host && info.port == other.port
} }
val address: String val address: String?
get() = info.host.hostAddress get() = info.host.hostAddress
val port: Int val port: Int
get() = info.port get() = info.port

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.setMonitorActivity(this@MonitorActivity)
} }
override fun onServiceDisconnected(className: ComponentName) { override fun onServiceDisconnected(className: ComponentName) {
@@ -85,8 +85,9 @@ class MonitorActivity : Activity() {
val linkAddresses = cm.getLinkProperties(network)!!.linkAddresses val linkAddresses = cm.getLinkProperties(network)!!.linkAddresses
for (linkAddress in linkAddresses) { for (linkAddress in linkAddresses) {
val address = linkAddress.address val address = linkAddress.address
if (!address.isLinkLocalAddress && !address.isLoopbackAddress) { val hostAddress = address.hostAddress
listenAddresses.add(address.hostAddress + " (" + networkInfo.typeName + ")") if (!address.isLinkLocalAddress && !address.isLoopbackAddress && hostAddress != null) {
listenAddresses.add(hostAddress + " (" + networkInfo.typeName + ")")
} }
} }
} }

View File

@@ -33,7 +33,7 @@ class StartActivity : Activity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_start) setContentView(R.layout.activity_start)
val monitorButton = findViewById<Button>(R.id.useChildDevice) val monitorButton = findViewById<Button>(R.id.useChildDevice)
monitorButton.setOnClickListener { v: View? -> monitorButton.setOnClickListener { _: View? ->
Log.i(TAG, "Starting up monitor") Log.i(TAG, "Starting up monitor")
if (isAudioRecordingPermissionGranted) { if (isAudioRecordingPermissionGranted) {
startActivity(Intent(applicationContext, MonitorActivity::class.java)) startActivity(Intent(applicationContext, MonitorActivity::class.java))
@@ -42,7 +42,7 @@ class StartActivity : Activity() {
} }
} }
val connectButton = findViewById<Button>(R.id.useParentDevice) val connectButton = findViewById<Button>(R.id.useParentDevice)
connectButton.setOnClickListener { v: View? -> connectButton.setOnClickListener { _: View? ->
Log.i(TAG, "Starting connection activity") Log.i(TAG, "Starting connection activity")
if (isMulticastPermissionGranted) { if (isMulticastPermissionGranted) {
val i = Intent(applicationContext, DiscoverActivity::class.java) val i = Intent(applicationContext, DiscoverActivity::class.java)