generated from JetBrains/compose-multiplatform-template
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from DanicMa/custom-android-chrome-client
Custom android chrome client (allowing for file selection)
- Loading branch information
Showing
10 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
sample/shared/src/androidMain/kotlin/com/kevinnzou/sample/FileChooseWebViewSample.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.kevinnzou.sample | ||
|
||
import android.app.Activity | ||
import android.content.ActivityNotFoundException | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.webkit.ValueCallback | ||
import android.webkit.WebView | ||
import android.widget.Toast | ||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.activity.result.ActivityResult | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.multiplatform.webview.web.AccompanistWebChromeClient | ||
import com.multiplatform.webview.web.PlatformWebViewParams | ||
|
||
@Composable | ||
actual fun getPlatformWebViewParams(): PlatformWebViewParams? { | ||
var fileChooserIntent by remember { mutableStateOf<Intent?>(null) } | ||
|
||
val webViewChromeClient = | ||
remember { | ||
FileChoosableWebChromeClient(onShowFilePicker = { fileChooserIntent = it }) | ||
} | ||
|
||
val launcher = | ||
rememberLauncherForActivityResult( | ||
contract = ActivityResultContracts.StartActivityForResult(), | ||
) { result: ActivityResult -> | ||
if (result.resultCode != Activity.RESULT_OK) { | ||
Toast.makeText( | ||
webViewChromeClient.context, | ||
"resultCode is not RESULT_OK (value: ${result.resultCode})", | ||
Toast.LENGTH_SHORT, | ||
).show() | ||
webViewChromeClient.cancelFileChooser() | ||
return@rememberLauncherForActivityResult | ||
} | ||
|
||
val intent = result.data | ||
if (intent == null) { | ||
Toast.makeText( | ||
webViewChromeClient.context, | ||
"result intent is null", | ||
Toast.LENGTH_SHORT, | ||
).show() | ||
webViewChromeClient.cancelFileChooser() | ||
return@rememberLauncherForActivityResult | ||
} | ||
|
||
val singleFile: Uri? = intent.data | ||
val multiFiles: List<Uri>? = intent.getUris() | ||
|
||
when { | ||
singleFile != null -> webViewChromeClient.onReceiveFiles(arrayOf(singleFile)) | ||
multiFiles != null -> webViewChromeClient.onReceiveFiles(multiFiles.toTypedArray()) | ||
else -> { | ||
Toast.makeText( | ||
webViewChromeClient.context, | ||
"data and clipData is null", | ||
Toast.LENGTH_SHORT, | ||
).show() | ||
webViewChromeClient.cancelFileChooser() | ||
} | ||
} | ||
} | ||
|
||
LaunchedEffect(key1 = fileChooserIntent) { | ||
if (fileChooserIntent != null) { | ||
try { | ||
launcher.launch(fileChooserIntent) | ||
} catch (e: ActivityNotFoundException) { | ||
webViewChromeClient.cancelFileChooser() | ||
} | ||
} | ||
} | ||
|
||
return PlatformWebViewParams(chromeClient = webViewChromeClient) | ||
} | ||
|
||
private fun Intent.getUris(): List<Uri>? { | ||
val clipData = clipData ?: return null | ||
return (0 until clipData.itemCount).map { clipData.getItemAt(it).uri } | ||
} | ||
|
||
private class FileChoosableWebChromeClient( | ||
private val onShowFilePicker: (Intent) -> Unit, | ||
) : AccompanistWebChromeClient() { | ||
private var filePathCallback: ValueCallback<Array<Uri>>? = null | ||
|
||
override fun onShowFileChooser( | ||
webView: WebView?, | ||
filePathCallback: ValueCallback<Array<Uri>>?, | ||
fileChooserParams: FileChooserParams?, | ||
): Boolean { | ||
this.filePathCallback = filePathCallback | ||
val filePickerIntent = fileChooserParams?.createIntent() | ||
|
||
if (filePickerIntent == null) { | ||
cancelFileChooser() | ||
} else { | ||
onShowFilePicker(filePickerIntent) | ||
} | ||
return true | ||
} | ||
|
||
fun onReceiveFiles(uris: Array<Uri>) { | ||
filePathCallback?.onReceiveValue(uris) | ||
filePathCallback = null | ||
} | ||
|
||
fun cancelFileChooser() { | ||
filePathCallback?.onReceiveValue(null) | ||
filePathCallback = null | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
sample/shared/src/commonMain/kotlin/com/kevinnzou/sample/FileChooseWebViewSample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.kevinnzou.sample | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.IconButton | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TopAppBar | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBack | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.NavHostController | ||
import com.multiplatform.webview.util.KLogSeverity | ||
import com.multiplatform.webview.web.PlatformWebViewParams | ||
import com.multiplatform.webview.web.WebView | ||
import com.multiplatform.webview.web.rememberWebViewNavigator | ||
import com.multiplatform.webview.web.rememberWebViewStateWithHTMLFile | ||
|
||
/** | ||
* Created By briandr97 2024/8/8 | ||
* | ||
* Basic Sample for choose file in webview | ||
*/ | ||
@Composable | ||
internal fun FileChooseWebViewSample(navHostController: NavHostController? = null) { | ||
val webViewState = rememberWebViewStateWithHTMLFile(fileName = "fileChoose.html") | ||
val webViewNavigator = rememberWebViewNavigator() | ||
LaunchedEffect(Unit) { | ||
webViewState.webSettings.apply { | ||
zoomLevel = 1.0 | ||
logSeverity = KLogSeverity.Debug | ||
androidWebSettings.apply { | ||
isAlgorithmicDarkeningAllowed = true | ||
safeBrowsingEnabled = true | ||
allowFileAccess = false | ||
} | ||
} | ||
} | ||
MaterialTheme { | ||
Column { | ||
TopAppBar( | ||
title = { Text(text = "Html Sample") }, | ||
navigationIcon = { | ||
IconButton(onClick = { | ||
navHostController?.popBackStack() | ||
}) { | ||
Icon( | ||
imageVector = Icons.Default.ArrowBack, | ||
contentDescription = "Back", | ||
) | ||
} | ||
}, | ||
) | ||
|
||
Box(Modifier.fillMaxSize()) { | ||
WebView( | ||
state = webViewState, | ||
modifier = Modifier.fillMaxSize(), | ||
captureBackPresses = false, | ||
navigator = webViewNavigator, | ||
platformWebViewParams = getPlatformWebViewParams(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
expect fun getPlatformWebViewParams(): PlatformWebViewParams? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
sample/shared/src/commonMain/resources/assets/fileChoose.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<html> | ||
<head> | ||
<header> | ||
<meta name='viewport' | ||
content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'> | ||
</header> | ||
<link rel="stylesheet" type="text/css" href="styles.css"> | ||
<title>Compose WebView Multiplatform</title> | ||
</head> | ||
<body> | ||
<h1>Compose WebView Multiplatform</h1> | ||
<div> | ||
<text>image: </text> | ||
<input type="file" multiple name="image" id="imageChoose" accept="image/*"> | ||
</div> | ||
<div> | ||
<text>video: </text> | ||
<input type="file" multiple name="video" id="videoChoose" accept="video/*"> | ||
</div> | ||
<div> | ||
<text>audio: </text> | ||
<input type="file" multiple name="audio" id="audioChoose" accept="audio/*"> | ||
</div> | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
sample/shared/src/desktopMain/kotlin/com/kevinnzou/sample/FileChooseWebViewSample.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.kevinnzou.sample | ||
|
||
import androidx.compose.runtime.Composable | ||
import com.multiplatform.webview.web.PlatformWebViewParams | ||
|
||
@Composable | ||
actual fun getPlatformWebViewParams(): PlatformWebViewParams? = null |
7 changes: 7 additions & 0 deletions
7
sample/shared/src/iosMain/kotlin/com/kevinnzou/sample/FileChooseWebViewSample.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.kevinnzou.sample | ||
|
||
import androidx.compose.runtime.Composable | ||
import com.multiplatform.webview.web.PlatformWebViewParams | ||
|
||
@Composable | ||
actual fun getPlatformWebViewParams(): PlatformWebViewParams? = null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters