From 30cdcf50cc5a1a2126067683c2cad7f94bd9eb13 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sun, 12 Feb 2023 14:37:36 -0500 Subject: [PATCH] Prevent using InternalCoroutinesApi --- .../be/mygod/vpnhotspot/util/UpdateChecker.kt | 16 +++++----- .../java/be/mygod/vpnhotspot/util/Utils.kt | 31 +++++++++++++------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt b/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt index 61258473..69fe6576 100644 --- a/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt +++ b/mobile/src/freedom/java/be/mygod/vpnhotspot/util/UpdateChecker.kt @@ -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() - })) - } + reset = conn.getHeaderField("X-RateLimit-Reset")?.toLongOrNull() + conn.inputStream.bufferedReader().readText() + })) putString(KEY_VERSION, update?.let { putLong(KEY_PUBLISHED, update.published) it.message diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index 7b767381..d859931d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -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 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() } - try { - return block(conn) - } finally { - handle.dispose() - conn.disconnect() + return suspendCancellableCoroutine { cont -> + val job = GlobalScope.launch(Dispatchers.IO) { + try { + cont.resume(block(conn)) { cont.resumeWithException(it) } + } catch (e: Throwable) { + cont.resumeWithException(e) + } finally { + conn.disconnect() + } + } + cont.invokeOnCancellation { + job.cancel(it as? CancellationException) + conn.disconnect() + } } }