Migrate from deprecated APIs

This commit is contained in:
Mygod
2021-05-05 10:04:55 -04:00
parent 71bfeb059b
commit 5c4f88967b
6 changed files with 23 additions and 28 deletions

View File

@@ -11,10 +11,7 @@ import androidx.collection.LongSparseArray
import androidx.collection.set import androidx.collection.set
import androidx.collection.valueIterator import androidx.collection.valueIterator
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.*
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import java.io.* import java.io.*
@@ -77,15 +74,13 @@ class RootServer {
override fun shouldRemove(result: Byte) = result.toInt() != SUCCESS override fun shouldRemove(result: Byte) = result.toInt() != SUCCESS
override fun invoke(input: DataInputStream, result: Byte) { override fun invoke(input: DataInputStream, result: Byte) {
when (result.toInt()) { when (result.toInt()) {
// the channel we are supporting should never block SUCCESS -> channel.trySend(input.readParcelable(classLoader)).onClosed {
SUCCESS -> check(try {
channel.offer(input.readParcelable(classLoader))
} catch (closed: Throwable) {
active = false active = false
GlobalScope.launch(Dispatchers.Unconfined) { sendClosed() } GlobalScope.launch(Dispatchers.Unconfined) { sendClosed() }
finish.completeExceptionally(closed) finish.completeExceptionally(it
?: ClosedSendChannelException("Channel was closed normally"))
return return
}) }.onFailure { throw it!! } // the channel we are supporting should never block
CHANNEL_CONSUMED -> finish.complete(Unit) CHANNEL_CONSUMED -> finish.complete(Unit)
else -> finish.completeExceptionally(input.readException(result)) else -> finish.completeExceptionally(input.readException(result))
} }

View File

@@ -38,7 +38,7 @@ object ServiceNotification {
lines += context.getString(R.string.notification_interfaces_inactive, inactive.joinToString()) lines += context.getString(R.string.notification_interfaces_inactive, inactive.joinToString())
} }
return if (lines.size <= 1) builder.setContentText(lines.singleOrNull()).build() else { return if (lines.size <= 1) builder.setContentText(lines.singleOrNull()).build() else {
val deviceCount = deviceCounts.sumBy { it.value } val deviceCount = deviceCounts.sumOf { it.value }
val interfaceCount = deviceCounts.size + inactive.size val interfaceCount = deviceCounts.size + inactive.size
NotificationCompat.BigTextStyle(builder NotificationCompat.BigTextStyle(builder
.setContentText(context.resources.getQuantityString(R.plurals.notification_connected_devices, .setContentText(context.resources.getQuantityString(R.plurals.notification_connected_devices,

View File

@@ -8,7 +8,8 @@ import java.nio.ByteOrder
/** /**
* Compat support class for [MacAddress]. * Compat support class for [MacAddress].
*/ */
inline class MacAddressCompat(val addr: Long) { @JvmInline
value class MacAddressCompat(val addr: Long) {
companion object { companion object {
private const val ETHER_ADDR_LEN = 6 private const val ETHER_ADDR_LEN = 6
/** /**

View File

@@ -5,9 +5,7 @@ import be.mygod.vpnhotspot.net.IpNeighbour
import kotlinx.collections.immutable.PersistentMap import kotlinx.collections.immutable.PersistentMap
import kotlinx.collections.immutable.persistentMapOf import kotlinx.collections.immutable.persistentMapOf
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.*
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.channels.sendBlocking
class IpNeighbourMonitor private constructor() : IpMonitor() { class IpNeighbourMonitor private constructor() : IpMonitor() {
companion object { companion object {
@@ -69,7 +67,7 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
IpNeighbour.State.DELETING -> neighbours.remove(IpDev(neighbour)) IpNeighbour.State.DELETING -> neighbours.remove(IpDev(neighbour))
else -> neighbours.put(IpDev(neighbour), neighbour) else -> neighbours.put(IpDev(neighbour), neighbour)
} }
if (neighbours != old) aggregator.sendBlocking(neighbours) if (neighbours != old) aggregator.trySendBlocking(neighbours).onFailure { throw it!! }
} }
override fun processLines(lines: Sequence<String>) { override fun processLines(lines: Sequence<String>) {
@@ -78,6 +76,6 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
.filter { it.state != IpNeighbour.State.DELETING } // skip entries without lladdr .filter { it.state != IpNeighbour.State.DELETING } // skip entries without lladdr
.associateByTo(persistentMapOf<IpDev, IpNeighbour>().builder()) { IpDev(it) } .associateByTo(persistentMapOf<IpDev, IpNeighbour>().builder()) { IpDev(it) }
.build() .build()
aggregator.sendBlocking(neighbours) aggregator.trySendBlocking(neighbours).onFailure { throw it!! }
} }
} }

View File

@@ -13,6 +13,8 @@ import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.util.Services import be.mygod.vpnhotspot.util.Services
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.onClosed
import kotlinx.coroutines.channels.onFailure
import kotlinx.coroutines.channels.produce import kotlinx.coroutines.channels.produce
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import java.io.File import java.io.File
@@ -101,7 +103,7 @@ class ProcessListener(private val terminateRegex: Regex,
launch(parent) { launch(parent) {
try { try {
process.inputStream.bufferedReader().forEachLine { process.inputStream.bufferedReader().forEachLine {
check(offer(ProcessData.StdoutLine(it))) trySend(ProcessData.StdoutLine(it)).onClosed { return@forEachLine }.onFailure { throw it!! }
if (terminateRegex.containsMatchIn(it)) process.destroy() if (terminateRegex.containsMatchIn(it)) process.destroy()
} }
} catch (_: InterruptedIOException) { } } catch (_: InterruptedIOException) { }
@@ -111,7 +113,9 @@ class ProcessListener(private val terminateRegex: Regex,
process.errorStream.bufferedReader().forEachLine { check(offer(ProcessData.StderrLine(it))) } process.errorStream.bufferedReader().forEachLine { check(offer(ProcessData.StderrLine(it))) }
} catch (_: InterruptedIOException) { } } catch (_: InterruptedIOException) { }
} }
launch(parent) { check(offer(ProcessData.Exit(process.waitFor()))) } launch(parent) {
trySend(ProcessData.Exit(process.waitFor())).onClosed { return@launch }.onFailure { throw it!! }
}
parent.join() parent.join()
} finally { } finally {
parent.cancel() parent.cancel()

View File

@@ -10,9 +10,7 @@ import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.*
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.channels.produce
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import timber.log.Timber import timber.log.Timber
@@ -65,12 +63,11 @@ object WifiApCommands {
override fun create(scope: CoroutineScope) = scope.produce<SoftApCallbackParcel>(capacity = capacity) { override fun create(scope: CoroutineScope) = scope.produce<SoftApCallbackParcel>(capacity = capacity) {
val finish = CompletableDeferred<Unit>() val finish = CompletableDeferred<Unit>()
val key = WifiApManager.registerSoftApCallback(object : WifiApManager.SoftApCallbackCompat { val key = WifiApManager.registerSoftApCallback(object : WifiApManager.SoftApCallbackCompat {
private fun push(parcel: SoftApCallbackParcel) = check(try { private fun push(parcel: SoftApCallbackParcel) {
offer(parcel) trySend(parcel).onClosed {
} catch (closed: Throwable) { finish.completeExceptionally(it ?: ClosedSendChannelException("Channel was closed normally"))
finish.completeExceptionally(closed) }.onFailure { throw it!! }
true }
})
override fun onStateChanged(state: Int, failureReason: Int) = override fun onStateChanged(state: Int, failureReason: Int) =
push(SoftApCallbackParcel.OnStateChanged(state, failureReason)) push(SoftApCallbackParcel.OnStateChanged(state, failureReason))