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 ...@@ -23,19 +23,22 @@ package com.github.shadowsocks.bg
import android.annotation.TargetApi import android.annotation.TargetApi
import android.net.DnsResolver import android.net.DnsResolver
import android.net.Network import android.net.Network
import android.os.Handler import android.net.ParseException
import android.os.HandlerThread import android.os.CancellationSignal
import android.system.ErrnoException
import androidx.core.os.BuildCompat import androidx.core.os.BuildCompat
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import java.net.InetAddress import java.net.InetAddress
import java.util.concurrent.Executor
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
sealed class DnsResolverCompat { sealed class DnsResolverCompat {
companion object : 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) override suspend fun resolve(network: Network, host: String) = instance.resolve(network, host)
} }
...@@ -48,18 +51,22 @@ sealed class DnsResolverCompat { ...@@ -48,18 +51,22 @@ sealed class DnsResolverCompat {
} }
@TargetApi(29) @TargetApi(29)
private class DnsResolverCompat29 : DnsResolverCompat() { private object DnsResolverCompat29 : DnsResolverCompat() {
private val handler = Handler(HandlerThread("DnsResolverCompat").run { /**
start() * This executor will run on its caller directly. On Q beta 3, this is called in main thread.
looper */
}) private val executor = Executor { it.run() }
override suspend fun resolve(network: Network, host: String): Array<InetAddress> { override suspend fun resolve(network: Network, host: String): Array<InetAddress> {
return suspendCancellableCoroutine { cont -> return suspendCancellableCoroutine { cont ->
val signal = CancellationSignal()
cont.invokeOnCancellation { signal.cancel() }
// retry should be handled by client instead // retry should be handled by client instead
DnsResolver.getInstance().query(network, host, DnsResolver.FLAG_NO_RETRY, handler) { DnsResolver.getInstance().query(network, host, DnsResolver.FLAG_NO_RETRY, executor,
cont.resume(it.toTypedArray()) 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 @@ ...@@ -20,6 +20,7 @@
package com.github.shadowsocks.utils package com.github.shadowsocks.utils
import android.annotation.SuppressLint
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.ContentResolver import android.content.ContentResolver
import android.content.Context import android.content.Context
...@@ -28,7 +29,6 @@ import android.content.pm.PackageInfo ...@@ -28,7 +29,6 @@ import android.content.pm.PackageInfo
import android.content.res.Resources import android.content.res.Resources
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.ImageDecoder import android.graphics.ImageDecoder
import android.net.InetAddresses
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.system.Os import android.system.Os
...@@ -46,7 +46,7 @@ import java.net.InetAddress ...@@ -46,7 +46,7 @@ import java.net.InetAddress
val Throwable.readableMessage get() = localizedMessage ?: javaClass.name 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 { InetAddress::class.java.getDeclaredMethod("parseNumericAddress", String::class.java).apply {
isAccessible = true isAccessible = true
} }
...@@ -54,12 +54,11 @@ private val parseNumericAddress by lazy { ...@@ -54,12 +54,11 @@ private val parseNumericAddress by lazy {
/** /**
* A slightly more performant variant of parseNumericAddress. * 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) fun String?.parseNumericAddress(): InetAddress? = Os.inet_pton(OsConstants.AF_INET, this)
?: Os.inet_pton(OsConstants.AF_INET6, this)?.let { ?: Os.inet_pton(OsConstants.AF_INET6, this)?.let {
if (BuildCompat.isAtLeastQ()) InetAddresses.parseNumericAddress(this) if (BuildCompat.isAtLeastQ()) it else parseNumericAddress.invoke(null, this) as InetAddress
else parseNumericAddress.invoke(null, this) as InetAddress
} }
fun HttpURLConnection.disconnectFromMain() { 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