Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix system file browser not being invoked #1325

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.jellyfin.mobile.webapp

import android.content.Intent
import android.net.Uri
import android.util.Log
import android.webkit.ConsoleMessage
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import timber.log.Timber

class LoggingWebChromeClient : WebChromeClient() {
class JellyfinWebChromeClient(
private val fileChooserListener: FileChooserListener,
) : WebChromeClient() {
override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
val logLevel = when (consoleMessage.messageLevel()) {
ConsoleMessage.MessageLevel.ERROR -> Log.ERROR
Expand All @@ -25,4 +31,22 @@ class LoggingWebChromeClient : WebChromeClient() {

return true
}

override fun onShowFileChooser(
webView: WebView,
filePathCallback: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserParams?,
): Boolean {
if (fileChooserParams == null) {
filePathCallback.onReceiveValue(null)
return true
}

fileChooserListener.onShowFileChooser(fileChooserParams.createIntent(), filePathCallback)
return true
}

interface FileChooserListener {
fun onShowFileChooser(intent: Intent, filePathCallback: ValueCallback<Array<Uri>>)
}
}
21 changes: 19 additions & 2 deletions app/src/main/java/org/jellyfin/mobile/webapp/WebViewFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.ValueCallback
import android.webkit.WebChromeClient.FileChooserParams
import android.webkit.WebView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.view.ViewCompat
import androidx.core.view.doOnNextLayout
Expand Down Expand Up @@ -45,7 +49,7 @@ import org.jellyfin.mobile.utils.requestNoBatteryOptimizations
import org.jellyfin.mobile.utils.runOnUiThread
import org.koin.android.ext.android.inject

class WebViewFragment : Fragment(), BackPressInterceptor {
class WebViewFragment : Fragment(), BackPressInterceptor, JellyfinWebChromeClient.FileChooserListener {
val appPreferences: AppPreferences by inject()
private val apiClientController: ApiClientController by inject()
private val webappFunctionChannel: WebappFunctionChannel by inject()
Expand All @@ -67,6 +71,14 @@ class WebViewFragment : Fragment(), BackPressInterceptor {
// UI
private var webViewBinding: FragmentWebviewBinding? = null

// External file access
private var fileChooserActivityLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
) { result ->
fileChooserCallback?.onReceiveValue(FileChooserParams.parseResult(result.resultCode, result.data))
}
private var fileChooserCallback: ValueCallback<Array<Uri>>? = null

init {
enableServiceWorkerWorkaround()
}
Expand Down Expand Up @@ -175,7 +187,7 @@ class WebViewFragment : Fragment(), BackPressInterceptor {
return
}
webViewClient = jellyfinWebViewClient
webChromeClient = LoggingWebChromeClient()
webChromeClient = JellyfinWebChromeClient(this@WebViewFragment)
settings.applyDefault()
addJavascriptInterface(NativeInterface(requireContext()), "NativeInterface")
addJavascriptInterface(nativePlayer, "NativePlayer")
Expand Down Expand Up @@ -249,4 +261,9 @@ class WebViewFragment : Fragment(), BackPressInterceptor {
connected = false
onSelectServer(error = true)
}

override fun onShowFileChooser(intent: Intent, filePathCallback: ValueCallback<Array<Uri>>) {
fileChooserCallback = filePathCallback
fileChooserActivityLauncher.launch(intent)
}
}