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

View File

@@ -19,8 +19,11 @@ import androidx.fragment.app.FragmentManager
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.job
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import timber.log.Timber
import java.lang.invoke.MethodHandles
import java.lang.reflect.InvocationHandler
@@ -32,7 +35,7 @@ import java.net.NetworkInterface
import java.net.SocketException
import java.net.URL
import java.util.Locale
import kotlin.coroutines.coroutineContext
import kotlin.coroutines.resumeWithException
tailrec fun Throwable.getRootCause(): Throwable {
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)
}
@OptIn(InternalCoroutinesApi::class)
suspend fun <T> connectCancellable(url: String, block: suspend (HttpURLConnection) -> T): T {
@Suppress("BlockingMethodInNonBlockingContext")
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 {
return block(conn)
cont.resume(block(conn)) { cont.resumeWithException(it) }
} catch (e: Throwable) {
cont.resumeWithException(e)
} finally {
handle.dispose()
conn.disconnect()
}
}
cont.invokeOnCancellation {
job.cancel(it as? CancellationException)
conn.disconnect()
}
}
}