Commit fa6eee94 authored by Mygod's avatar Mygod

Update API to Q beta 3

parent d9430f50
......@@ -23,19 +23,22 @@ package com.github.shadowsocks.bg
import android.annotation.TargetApi
import android.net.DnsResolver
import android.net.Network
import android.os.Handler
import android.os.HandlerThread
import android.net.ParseException
import android.os.CancellationSignal
import android.system.ErrnoException
import androidx.core.os.BuildCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.suspendCancellableCoroutine
import java.net.InetAddress
import java.util.concurrent.Executor
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
sealed class DnsResolverCompat {
companion object : DnsResolverCompat() {
private val instance by lazy { if (BuildCompat.isAtLeastQ()) DnsResolverCompat29() else DnsResolverCompat21 }
private val instance by lazy { if (BuildCompat.isAtLeastQ()) DnsResolverCompat29 else DnsResolverCompat21 }
override suspend fun resolve(network: Network, host: String) = instance.resolve(network, host)
}
......@@ -48,18 +51,22 @@ sealed class DnsResolverCompat {
}
@TargetApi(29)
private class DnsResolverCompat29 : DnsResolverCompat() {
private val handler = Handler(HandlerThread("DnsResolverCompat").run {
start()
looper
})
private object DnsResolverCompat29 : DnsResolverCompat() {
/**
* This executor will run on its caller directly. On Q beta 3, this is called in main thread.
*/
private val executor = Executor { it.run() }
override suspend fun resolve(network: Network, host: String): Array<InetAddress> {
return suspendCancellableCoroutine { cont ->
val signal = CancellationSignal()
cont.invokeOnCancellation { signal.cancel() }
// retry should be handled by client instead
DnsResolver.getInstance().query(network, host, DnsResolver.FLAG_NO_RETRY, handler) {
cont.resume(it.toTypedArray())
}
DnsResolver.getInstance().query(network, host, DnsResolver.FLAG_NO_RETRY, executor,
signal, object : DnsResolver.InetAddressAnswerCallback() {
override fun onAnswer(answer: MutableList<InetAddress>) = cont.resume(answer.toTypedArray())
override fun onQueryException(exception: ErrnoException) = cont.resumeWithException(exception)
override fun onParseException(exception: ParseException) = cont.resumeWithException(exception)
})
}
}
}
......
......@@ -20,6 +20,7 @@
package com.github.shadowsocks.utils
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.ContentResolver
import android.content.Context
......@@ -28,7 +29,6 @@ import android.content.pm.PackageInfo
import android.content.res.Resources
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.net.InetAddresses
import android.net.Uri
import android.os.Build
import android.system.Os
......@@ -46,7 +46,7 @@ import java.net.InetAddress
val Throwable.readableMessage get() = localizedMessage ?: javaClass.name
private val parseNumericAddress by lazy {
private val parseNumericAddress by lazy @SuppressLint("DiscouragedPrivateApi") {
InetAddress::class.java.getDeclaredMethod("parseNumericAddress", String::class.java).apply {
isAccessible = true
}
......@@ -54,12 +54,11 @@ private val parseNumericAddress by lazy {
/**
* A slightly more performant variant of parseNumericAddress.
*
* Bug: https://issuetracker.google.com/issues/123456213
* Bug in Android 9.0 and lower: https://issuetracker.google.com/issues/123456213
*/
fun String?.parseNumericAddress(): InetAddress? = Os.inet_pton(OsConstants.AF_INET, this)
?: Os.inet_pton(OsConstants.AF_INET6, this)?.let {
if (BuildCompat.isAtLeastQ()) InetAddresses.parseNumericAddress(this)
else parseNumericAddress.invoke(null, this) as InetAddress
if (BuildCompat.isAtLeastQ()) it else parseNumericAddress.invoke(null, this) as InetAddress
}
fun HttpURLConnection.disconnectFromMain() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment