Skip to content

Commit cd4a719

Browse files
committed
Fix connection being disconnected
1 parent 3325157 commit cd4a719

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Other features:
3939
]
4040
```
4141
where the fingerprint can be obtained via `keytool -list -v -keystore /path/to/keystore`.
42-
(If you prefer to using Android Studio to create this file with a wizard, see [here]( https://developer.android.com/studio/write/app-link-indexing#associatesite) for instructions.)
42+
(If you prefer to using Android Studio to create this file with a wizard, see [here](https://developer.android.com/studio/write/app-link-indexing#associatesite) for instructions.)
4343
3. (Optional) Make modifications to the source code if you wish.
4444

4545
### Build instructions

app/src/main/java/be/mygod/reactmap/webkit/ReactMapFragment.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import be.mygod.reactmap.util.CreateDynamicDocument
3636
import be.mygod.reactmap.util.findErrorStream
3737
import com.google.firebase.analytics.FirebaseAnalytics
3838
import kotlinx.coroutines.launch
39-
import kotlinx.coroutines.runBlocking
4039
import org.json.JSONArray
4140
import org.json.JSONException
4241
import org.json.JSONObject
@@ -210,11 +209,9 @@ class ReactMapFragment @JvmOverloads constructor(private val overrideUri: Uri? =
210209

211210
private fun buildResponse(request: WebResourceRequest, transform: (Reader) -> String) = try {
212211
val url = request.url.toString()
213-
val conn = runBlocking {
214-
ReactMapHttpEngine.connectWithCookie(url) { conn ->
215-
conn.requestMethod = request.method
216-
for ((key, value) in request.requestHeaders) conn.addRequestProperty(key, value)
217-
}
212+
val conn = ReactMapHttpEngine.connectWithCookie(url) { conn ->
213+
conn.requestMethod = request.method
214+
for ((key, value) in request.requestHeaders) conn.addRequestProperty(key, value)
218215
}
219216
WebResourceResponse(conn.contentType?.substringBefore(';'), conn.contentEncoding, conn.responseCode,
220217
conn.responseMessage.let { if (it.isNullOrBlank()) "N/A" else it },

app/src/main/java/be/mygod/reactmap/webkit/ReactMapHttpEngine.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ object ReactMapHttpEngine {
4343
path("/graphql")
4444
}.build().toString()
4545

46+
private fun openConnection(url: String) = (if (Build.VERSION.SDK_INT >= 34 || Build.VERSION.SDK_INT >=
47+
Build.VERSION_CODES.S && SdkExtensions.getExtensionVersion(Build.VERSION_CODES.S) >= 7) {
48+
engine.openConnection(URL(url))
49+
} else URL(url).openConnection()) as HttpURLConnection
4650
suspend fun <T> connectCancellable(url: String, block: suspend (HttpURLConnection) -> T): T {
47-
val conn = (if (Build.VERSION.SDK_INT >= 34 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
48-
SdkExtensions.getExtensionVersion(Build.VERSION_CODES.S) >= 7) {
49-
engine.openConnection(URL(url))
50-
} else @Suppress("BlockingMethodInNonBlockingContext") URL(url).openConnection()) as HttpURLConnection
51+
val conn = openConnection(url)
5152
return suspendCancellableCoroutine { cont ->
5253
val job = GlobalScope.launch(Dispatchers.IO) {
5354
try {
@@ -65,7 +66,7 @@ object ReactMapHttpEngine {
6566
}
6667
}
6768

68-
suspend fun connectWithCookie(url: String, setup: (HttpURLConnection) -> Unit) = connectCancellable(url) { conn ->
69+
fun connectWithCookie(url: String, setup: (HttpURLConnection) -> Unit) = openConnection(url).also { conn ->
6970
if (app.userManager.isUserUnlocked) {
7071
val cookie = CookieManager.getInstance()
7172
cookie.getCookie(url)?.let { conn.addRequestProperty("Cookie", it) }
@@ -75,7 +76,6 @@ object ReactMapHttpEngine {
7576
app.pref.getString(KEY_COOKIE, null)?.let { conn.addRequestProperty("Cookie", it) }
7677
setup(conn)
7778
}
78-
conn
7979
}
8080

8181
fun updateCookie() = app.pref.edit {

0 commit comments

Comments
 (0)