Prevent using InternalCoroutinesApi

This commit is contained in:
Mygod
2023-02-12 14:37:36 -05:00
parent 2fe4ad1806
commit 30cdcf50cc
2 changed files with 28 additions and 19 deletions

View File

@@ -5,14 +5,14 @@ import android.net.Uri
import androidx.core.content.edit import androidx.core.content.edit
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.BuildConfig import be.mygod.vpnhotspot.BuildConfig
import kotlinx.coroutines.* import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.cancellable import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import org.json.JSONArray import org.json.JSONArray
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.time.Instant import java.time.Instant
import java.util.concurrent.CancellationException import java.util.concurrent.CancellationException
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@@ -84,14 +84,12 @@ object UpdateChecker {
var reset: Long? = null var reset: Long? = null
app.pref.edit { app.pref.edit {
try { try {
val update = connectCancellable( val update = findUpdate(JSONArray(connectCancellable(
"https://api.github.com/repos/Mygod/VPNHotspot/releases?per_page=100") { conn -> "https://api.github.com/repos/Mygod/VPNHotspot/releases?per_page=100") { conn ->
conn.setRequestProperty("Accept", "application/vnd.github.v3+json") conn.setRequestProperty("Accept", "application/vnd.github.v3+json")
findUpdate(JSONArray(withContext(Dispatchers.IO) {
reset = conn.getHeaderField("X-RateLimit-Reset")?.toLongOrNull() reset = conn.getHeaderField("X-RateLimit-Reset")?.toLongOrNull()
conn.inputStream.bufferedReader().readText() conn.inputStream.bufferedReader().readText()
})) }))
}
putString(KEY_VERSION, update?.let { putString(KEY_VERSION, update?.let {
putLong(KEY_PUBLISHED, update.published) putLong(KEY_PUBLISHED, update.published)
it.message it.message

View File

@@ -19,8 +19,11 @@ import androidx.fragment.app.FragmentManager
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.MacAddressCompat import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.InternalCoroutinesApi import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.job import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import timber.log.Timber import timber.log.Timber
import java.lang.invoke.MethodHandles import java.lang.invoke.MethodHandles
import java.lang.reflect.InvocationHandler import java.lang.reflect.InvocationHandler
@@ -32,7 +35,7 @@ import java.net.NetworkInterface
import java.net.SocketException import java.net.SocketException
import java.net.URL import java.net.URL
import java.util.Locale import java.util.Locale
import kotlin.coroutines.coroutineContext import kotlin.coroutines.resumeWithException
tailrec fun Throwable.getRootCause(): Throwable { tailrec fun Throwable.getRootCause(): Throwable {
if (this is InvocationTargetException || this is RemoteException) return (cause ?: return this).getRootCause() if (this is InvocationTargetException || this is RemoteException) return (cause ?: return this).getRootCause()
@@ -241,14 +244,22 @@ fun globalNetworkRequestBuilder() = NetworkRequest.Builder().apply {
if (Build.VERSION.SDK_INT >= 31) setIncludeOtherUidNetworks(true) if (Build.VERSION.SDK_INT >= 31) setIncludeOtherUidNetworks(true)
} }
@OptIn(InternalCoroutinesApi::class)
suspend fun <T> connectCancellable(url: String, block: suspend (HttpURLConnection) -> T): T { suspend fun <T> connectCancellable(url: String, block: suspend (HttpURLConnection) -> T): T {
@Suppress("BlockingMethodInNonBlockingContext")
val conn = URL(url).openConnection() as HttpURLConnection val conn = URL(url).openConnection() as HttpURLConnection
val handle = coroutineContext.job.invokeOnCompletion(true) { conn.disconnect() } return suspendCancellableCoroutine { cont ->
val job = GlobalScope.launch(Dispatchers.IO) {
try { try {
return block(conn) cont.resume(block(conn)) { cont.resumeWithException(it) }
} catch (e: Throwable) {
cont.resumeWithException(e)
} finally { } finally {
handle.dispose()
conn.disconnect() conn.disconnect()
} }
}
cont.invokeOnCancellation {
job.cancel(it as? CancellationException)
conn.disconnect()
}
}
} }