Remove unnecessary try-catch

This commit is contained in:
Mygod
2021-06-04 02:06:06 -04:00
parent 880843c8cb
commit 02e7f06e61

View File

@@ -394,41 +394,39 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
} }
val networkName = networkName val networkName = networkName
val passphrase = passphrase val passphrase = passphrase
try { @SuppressLint("MissingPermission") // missing permission will simply leading to returning ERROR
if (!safeMode || networkName == null || passphrase == null) { if (!safeMode || networkName == null || passphrase == null) {
persistNextGroup = true persistNextGroup = true
p2pManager.createGroup(channel, listener) p2pManager.createGroup(channel, listener)
} else @TargetApi(29) { } else @TargetApi(29) {
p2pManager.createGroup(channel, WifiP2pConfig.Builder().apply { p2pManager.createGroup(channel, WifiP2pConfig.Builder().apply {
try {
mNetworkName.set(this, networkName) // bypass networkName check
} catch (e: ReflectiveOperationException) {
Timber.w(e)
try { try {
mNetworkName.set(this, networkName) // bypass networkName check
} catch (e: ReflectiveOperationException) {
Timber.w(e)
setNetworkName(networkName) setNetworkName(networkName)
} catch (e: IllegalArgumentException) {
Timber.w(e)
return startFailure(e.readableMessage)
} }
setPassphrase(passphrase) }
when (val oc = operatingChannel) { setPassphrase(passphrase)
0 -> setGroupOperatingBand(when (val band = operatingBand) { when (val oc = operatingChannel) {
SoftApConfigurationCompat.BAND_2GHZ -> WifiP2pConfig.GROUP_OWNER_BAND_2GHZ 0 -> setGroupOperatingBand(when (val band = operatingBand) {
SoftApConfigurationCompat.BAND_5GHZ -> WifiP2pConfig.GROUP_OWNER_BAND_5GHZ SoftApConfigurationCompat.BAND_2GHZ -> WifiP2pConfig.GROUP_OWNER_BAND_2GHZ
else -> { SoftApConfigurationCompat.BAND_5GHZ -> WifiP2pConfig.GROUP_OWNER_BAND_5GHZ
require(SoftApConfigurationCompat.isLegacyEitherBand(band)) { "Unknown band $band" }
WifiP2pConfig.GROUP_OWNER_BAND_AUTO
}
})
else -> { else -> {
setGroupOperatingFrequency(SoftApConfigurationCompat.channelToFrequency(operatingBand, oc)) require(SoftApConfigurationCompat.isLegacyEitherBand(band)) { "Unknown band $band" }
WifiP2pConfig.GROUP_OWNER_BAND_AUTO
} }
})
else -> {
setGroupOperatingFrequency(SoftApConfigurationCompat.channelToFrequency(operatingBand, oc))
} }
setDeviceAddress(deviceAddress?.toPlatform()) }
}.build(), listener) setDeviceAddress(deviceAddress?.toPlatform())
} }.build(), listener)
} catch (e: SecurityException) {
Timber.w(e)
startFailure(e.readableMessage)
} catch (e: IllegalArgumentException) {
Timber.w(e)
startFailure(e.readableMessage)
} }
} }
/** /**
@@ -481,19 +479,17 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
private fun showNotification(group: WifiP2pGroup? = null) = ServiceNotification.startForeground(this, private fun showNotification(group: WifiP2pGroup? = null) = ServiceNotification.startForeground(this,
if (group == null) emptyMap() else mapOf(Pair(group.`interface`, group.clientList?.size ?: 0))) if (group == null) emptyMap() else mapOf(Pair(group.`interface`, group.clientList?.size ?: 0)))
private fun removeGroup() { private fun removeGroup() = p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener {
p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener { override fun onSuccess() {
override fun onSuccess() { launch { cleanLocked() }
launch { cleanLocked() } }
} override fun onFailure(reason: Int) {
override fun onFailure(reason: Int) { if (reason != WifiP2pManager.BUSY) {
if (reason != WifiP2pManager.BUSY) { SmartSnackbar.make(formatReason(R.string.repeater_remove_group_failure, reason)).show()
SmartSnackbar.make(formatReason(R.string.repeater_remove_group_failure, reason)).show() } // else assuming it's already gone
} // else assuming it's already gone onSuccess()
launch { cleanLocked() } }
} })
})
}
private fun cleanLocked() { private fun cleanLocked() {
if (receiverRegistered) { if (receiverRegistered) {
ensureReceiverUnregistered(receiver) ensureReceiverUnregistered(receiver)