AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/WebviewFragment.kt

84 lines
2.8 KiB
Kotlin
Raw Normal View History

2022-08-15 23:24:19 +00:00
package com.lagradost.cloudstream3.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.JavascriptInterface
2022-08-15 23:24:19 +00:00
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
2022-08-21 18:14:26 +00:00
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
2022-08-15 23:24:19 +00:00
import androidx.navigation.fragment.findNavController
import com.lagradost.cloudstream3.MainActivity
import com.lagradost.cloudstream3.USER_AGENT
2023-07-14 00:28:49 +00:00
import com.lagradost.cloudstream3.databinding.FragmentWebviewBinding
2022-08-21 18:14:26 +00:00
import com.lagradost.cloudstream3.network.WebViewResolver
2022-08-15 23:24:19 +00:00
import com.lagradost.cloudstream3.utils.AppUtils.loadRepository
2023-07-14 00:28:49 +00:00
2022-08-15 23:24:19 +00:00
class WebviewFragment : Fragment() {
2023-07-14 00:28:49 +00:00
var binding: FragmentWebviewBinding? = null
2022-08-15 23:24:19 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val url = arguments?.getString(WEBVIEW_URL) ?: "".also {
findNavController().popBackStack()
}
2023-07-14 00:28:49 +00:00
binding?.webView?.webViewClient = object : WebViewClient() {
2022-08-15 23:24:19 +00:00
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
val requestUrl = request?.url.toString()
val performedAction = MainActivity.handleAppIntentUrl(activity, requestUrl, true)
if (performedAction) {
2022-08-15 23:24:19 +00:00
findNavController().popBackStack()
return true
}
return super.shouldOverrideUrlLoading(view, request)
}
}
2023-07-14 00:28:49 +00:00
binding?.webView?.apply {
WebViewResolver.webViewUserAgent = settings.userAgentString
2023-07-14 00:28:49 +00:00
addJavascriptInterface(RepoApi(activity), "RepoApi")
settings.javaScriptEnabled = true
settings.userAgentString = USER_AGENT
settings.domStorageEnabled = true
// WebView.setWebContentsDebuggingEnabled(true)
2022-08-21 18:14:26 +00:00
2023-07-14 00:28:49 +00:00
loadUrl(url)
}
2022-08-15 23:24:19 +00:00
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
2023-07-14 00:28:49 +00:00
): View {
val localBinding = FragmentWebviewBinding.inflate(inflater, container, false)
binding = localBinding
2022-08-15 23:24:19 +00:00
// Inflate the layout for this fragment
2023-07-14 00:28:49 +00:00
return localBinding.root//inflater.inflate(R.layout.fragment_webview, container, false)
2022-08-15 23:24:19 +00:00
}
companion object {
private const val WEBVIEW_URL = "webview_url"
fun newInstance(webViewUrl: String) =
Bundle().apply {
putString(WEBVIEW_URL, webViewUrl)
}
}
private class RepoApi(val activity: FragmentActivity?) {
@JavascriptInterface
2023-07-14 00:28:49 +00:00
fun installRepo(repoUrl: String) {
activity?.loadRepository(repoUrl)
}
}
2022-08-15 23:24:19 +00:00
}