Skip to content

Commit 53d23cc

Browse files
committed
fix: address issues with file uploads
1 parent 834a4c1 commit 53d23cc

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

app/src/main/java/com/mrepol742/webappp/MainActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import android.webkit.WebView
66
import androidx.activity.ComponentActivity
77
import androidx.activity.compose.setContent
88
import androidx.activity.enableEdgeToEdge
9+
import androidx.activity.result.contract.ActivityResultContracts
910
import androidx.compose.foundation.layout.fillMaxSize
1011
import androidx.compose.foundation.layout.padding
1112
import androidx.compose.foundation.layout.systemBarsPadding
1213
import androidx.compose.material3.Scaffold
1314
import androidx.compose.runtime.mutableStateOf
1415
import androidx.compose.ui.Modifier
16+
import com.mrepol742.webappp.client.SecureChromeClient
1517
import com.mrepol742.webappp.ui.theme.WebApppTheme
1618
import com.mrepol742.webappp.utils.DynamicShortcut
1719

@@ -27,6 +29,12 @@ class MainActivity : ComponentActivity() {
2729
"/contact-me" to "Contact Me",
2830
)
2931
private val webViewState = mutableStateOf<WebView?>(null)
32+
private val secureWebChromeClientState = mutableStateOf<SecureChromeClient?>(null)
33+
34+
private val fileChooserLauncher =
35+
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
36+
secureWebChromeClientState.value?.handleFileChosen(result.resultCode, result.data)
37+
}
3038

3139
override fun onCreate(savedInstanceState: Bundle?) {
3240
super.onCreate(savedInstanceState)
@@ -39,6 +47,8 @@ class MainActivity : ComponentActivity() {
3947
allowedDomain = allowedDomain,
4048
initialUrl = currentUrl,
4149
webViewState = webViewState,
50+
secureWebChromeClientState = secureWebChromeClientState,
51+
fileChooserLauncher= fileChooserLauncher,
4252
modifier = Modifier.padding(innerPadding)
4353
)
4454
}

app/src/main/java/com/mrepol742/webappp/WebViewScreen.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package com.mrepol742.webappp
22

33
import android.annotation.SuppressLint
44
import android.app.Activity
5+
import android.content.Intent
56
import android.util.Log
67
import android.view.ViewGroup
78
import android.webkit.WebView
89
import androidx.activity.compose.BackHandler
10+
import androidx.activity.result.ActivityResultLauncher
911
import androidx.compose.foundation.layout.fillMaxSize
1012
import androidx.compose.runtime.Composable
1113
import androidx.compose.runtime.LaunchedEffect
@@ -23,7 +25,14 @@ import kotlinx.coroutines.delay
2325

2426
@SuppressLint("SetJavaScriptEnabled")
2527
@Composable
26-
fun WebViewScreen(allowedDomain: String, initialUrl: String, webViewState: MutableState<WebView?>, modifier: Modifier = Modifier) {
28+
fun WebViewScreen(
29+
allowedDomain: String,
30+
initialUrl: String,
31+
webViewState: MutableState<WebView?>,
32+
secureWebChromeClientState: MutableState<SecureChromeClient?>,
33+
fileChooserLauncher: ActivityResultLauncher<Intent>,
34+
modifier: Modifier = Modifier
35+
) {
2736

2837
AndroidView(
2938
modifier = Modifier.fillMaxSize(),
@@ -64,7 +73,10 @@ fun WebViewScreen(allowedDomain: String, initialUrl: String, webViewState: Mutab
6473
setLayerType(android.view.View.LAYER_TYPE_HARDWARE, null)
6574

6675
webViewClient = SecureWebViewClient(context, allowedDomain)
67-
webChromeClient = SecureChromeClient(context as Activity)
76+
77+
val chromeClient = SecureChromeClient(context as Activity, fileChooserLauncher)
78+
webChromeClient = chromeClient
79+
secureWebChromeClientState.value = chromeClient
6880
setDownloadListener(DownloadListener(context))
6981

7082
loadUrl(initialUrl)

app/src/main/java/com/mrepol742/webappp/client/SecureChromeClient.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,18 @@ import android.webkit.ValueCallback
1212
import android.webkit.WebChromeClient
1313
import android.webkit.WebView
1414
import android.widget.FrameLayout
15+
import androidx.activity.result.ActivityResultLauncher
1516

1617
class SecureChromeClient(
17-
private val activity: Activity
18+
private val activity: Activity,
19+
private val fileChooserLauncher: ActivityResultLauncher<Intent>
1820
) : WebChromeClient() {
1921

2022
private var filePathCallback: ValueCallback<Array<Uri>>? = null
21-
private val fileChooserRequestCode = 1001
2223
private var customView: View? = null
2324
private var customViewCallback: WebChromeClient.CustomViewCallback? = null
2425
private var fullScreenContainer: FrameLayout? = null
2526

26-
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
27-
if (requestCode == fileChooserRequestCode) {
28-
val result: Array<Uri>? = if (resultCode == Activity.RESULT_OK && data != null) {
29-
data.data?.let { arrayOf(it) }
30-
} else null
31-
32-
filePathCallback?.onReceiveValue(result)
33-
filePathCallback = null
34-
}
35-
}
36-
3727
override fun onShowFileChooser(
3828
webView: WebView?,
3929
filePathCallback: ValueCallback<Array<Uri>>,
@@ -42,16 +32,26 @@ class SecureChromeClient(
4232
this.filePathCallback?.onReceiveValue(null)
4333
this.filePathCallback = filePathCallback
4434

45-
val intent = fileChooserParams.createIntent()
4635
return try {
47-
activity.startActivityForResult(intent, fileChooserRequestCode)
36+
val intent = fileChooserParams.createIntent()
37+
fileChooserLauncher.launch(intent) // ✅ modern way
4838
true
4939
} catch (e: Exception) {
5040
this.filePathCallback = null
5141
false
5242
}
5343
}
5444

45+
fun handleFileChosen(resultCode: Int, data: Intent?) {
46+
val result: Array<Uri>? =
47+
if (resultCode == Activity.RESULT_OK && data?.data != null) {
48+
arrayOf(data.data!!)
49+
} else null
50+
51+
filePathCallback?.onReceiveValue(result)
52+
filePathCallback = null
53+
}
54+
5555
override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
5656
// If a view already exists, hide it first
5757
if (customView != null) {

0 commit comments

Comments
 (0)