From d992d63c5f63e771b54100ae760e5bd6c4638d85 Mon Sep 17 00:00:00 2001 From: LagradOst <11805592+LagradOst@users.noreply.github.com> Date: Fri, 18 Feb 2022 22:20:35 +0100 Subject: [PATCH] fixed minor crashes --- .../cloudstream3/network/WebViewResolver.kt | 220 +++++++++--------- .../cloudstream3/ui/player/GeneratorPlayer.kt | 28 ++- .../ui/settings/SettingsFragment.kt | 6 +- 3 files changed, 135 insertions(+), 119 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt b/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt index 095bb3f2..2b55f90d 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt @@ -6,6 +6,7 @@ import android.webkit.* import com.lagradost.cloudstream3.AcraApplication import com.lagradost.cloudstream3.USER_AGENT import com.lagradost.cloudstream3.app +import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.utils.Coroutines.main import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking @@ -60,121 +61,128 @@ class WebViewResolver(val interceptUrl: Regex, val additionalUrls: List = main { // Useful for debugging // WebView.setWebContentsDebuggingEnabled(true) - webView = WebView( - AcraApplication.context - ?: throw RuntimeException("No base context in WebViewResolver") - ).apply { - // Bare minimum to bypass captcha - settings.javaScriptEnabled = true - settings.domStorageEnabled = true - settings.userAgentString = USER_AGENT - // Blocks unnecessary images, remove if captcha fucks. - settings.blockNetworkImage = true - } + try { + webView = WebView( + AcraApplication.context + ?: throw RuntimeException("No base context in WebViewResolver") + ).apply { + // Bare minimum to bypass captcha + settings.javaScriptEnabled = true + settings.domStorageEnabled = true + settings.userAgentString = USER_AGENT + // Blocks unnecessary images, remove if captcha fucks. + settings.blockNetworkImage = true + } - webView?.webViewClient = object : WebViewClient() { - override fun shouldInterceptRequest( - view: WebView, - request: WebResourceRequest - ): WebResourceResponse? = runBlocking { - val webViewUrl = request.url.toString() + webView?.webViewClient = object : WebViewClient() { + override fun shouldInterceptRequest( + view: WebView, + request: WebResourceRequest + ): WebResourceResponse? = runBlocking { + val webViewUrl = request.url.toString() // println("Loading WebView URL: $webViewUrl") - if (interceptUrl.containsMatchIn(webViewUrl)) { - fixedRequest = request.toRequest().also { - if (requestCallBack(it)) destroyWebView() + if (interceptUrl.containsMatchIn(webViewUrl)) { + fixedRequest = request.toRequest().also { + if (requestCallBack(it)) destroyWebView() + } + println("Web-view request finished: $webViewUrl") + destroyWebView() + return@runBlocking null + } + + if (additionalUrls.any { it.containsMatchIn(webViewUrl) }) { + extraRequestList.add(request.toRequest().also { + if (requestCallBack(it)) destroyWebView() + }) + } + + // Suppress image requests as we don't display them anywhere + // Less data, low chance of causing issues. + // blockNetworkImage also does this job but i will keep it for the future. + val blacklistedFiles = listOf( + ".jpg", + ".png", + ".webp", + ".mpg", + ".mpeg", + ".jpeg", + ".webm", + ".mp4", + ".mp3", + ".gifv", + ".flv", + ".asf", + ".mov", + ".mng", + ".mkv", + ".ogg", + ".avi", + ".wav", + ".woff2", + ".woff", + ".ttf", + ".css", + ".vtt", + ".srt", + ".ts", + ".gif", + // Warning, this might fuck some future sites, but it's used to make Sflix work. + "wss://" + ) + + /** NOTE! request.requestHeaders is not perfect! + * They don't contain all the headers the browser actually gives. + * Overriding with okhttp might fuck up otherwise working requests, + * e.g the recaptcha request. + * **/ + + return@runBlocking try { + when { + blacklistedFiles.any { URI(webViewUrl).path.contains(it) } || webViewUrl.endsWith( + "/favicon.ico" + ) -> WebResourceResponse( + "image/png", + null, + null + ) + + webViewUrl.contains("recaptcha") -> super.shouldInterceptRequest( + view, + request + ) + + request.method == "GET" -> app.get( + webViewUrl, + headers = request.requestHeaders + ).response.toWebResourceResponse() + + request.method == "POST" -> app.post( + webViewUrl, + headers = request.requestHeaders + ).response.toWebResourceResponse() + else -> return@runBlocking super.shouldInterceptRequest( + view, + request + ) + } + } catch (e: Exception) { + null } - println("Web-view request finished: $webViewUrl") - destroyWebView() - return@runBlocking null } - if (additionalUrls.any { it.containsMatchIn(webViewUrl) }) { - extraRequestList.add(request.toRequest().also { - if (requestCallBack(it)) destroyWebView() - }) - } - - // Suppress image requests as we don't display them anywhere - // Less data, low chance of causing issues. - // blockNetworkImage also does this job but i will keep it for the future. - val blacklistedFiles = listOf( - ".jpg", - ".png", - ".webp", - ".mpg", - ".mpeg", - ".jpeg", - ".webm", - ".mp4", - ".mp3", - ".gifv", - ".flv", - ".asf", - ".mov", - ".mng", - ".mkv", - ".ogg", - ".avi", - ".wav", - ".woff2", - ".woff", - ".ttf", - ".css", - ".vtt", - ".srt", - ".ts", - ".gif", - // Warning, this might fuck some future sites, but it's used to make Sflix work. - "wss://" - ) - - /** NOTE! request.requestHeaders is not perfect! - * They don't contain all the headers the browser actually gives. - * Overriding with okhttp might fuck up otherwise working requests, - * e.g the recaptcha request. - * **/ - - return@runBlocking try { - when { - blacklistedFiles.any { URI(webViewUrl).path.contains(it) } || webViewUrl.endsWith( - "/favicon.ico" - ) -> WebResourceResponse( - "image/png", - null, - null - ) - - webViewUrl.contains("recaptcha") -> super.shouldInterceptRequest( - view, - request - ) - - request.method == "GET" -> app.get( - webViewUrl, - headers = request.requestHeaders - ).response.toWebResourceResponse() - - request.method == "POST" -> app.post( - webViewUrl, - headers = request.requestHeaders - ).response.toWebResourceResponse() - else -> return@runBlocking super.shouldInterceptRequest(view, request) - } - } catch (e: Exception) { - null + override fun onReceivedSslError( + view: WebView?, + handler: SslErrorHandler?, + error: SslError? + ) { + handler?.proceed() // Ignore ssl issues } } - - override fun onReceivedSslError( - view: WebView?, - handler: SslErrorHandler?, - error: SslError? - ) { - handler?.proceed() // Ignore ssl issues - } + webView?.loadUrl(url, headers.toMap()) + } catch (e: Exception) { + logError(e) } - webView?.loadUrl(url, headers.toMap()) } var loop = 0 diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt index ccc49e01..6b809a39 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt @@ -142,19 +142,23 @@ class GeneratorPlayer : FullScreenPlayer() { } private fun openSubPicker() { - subsPathPicker.launch( - arrayOf( - "text/plain", - "text/str", - "application/octet-stream", - MimeTypes.TEXT_UNKNOWN, - MimeTypes.TEXT_VTT, - MimeTypes.TEXT_SSA, - MimeTypes.APPLICATION_TTML, - MimeTypes.APPLICATION_MP4VTT, - MimeTypes.APPLICATION_SUBRIP, + try { + subsPathPicker.launch( + arrayOf( + "text/plain", + "text/str", + "application/octet-stream", + MimeTypes.TEXT_UNKNOWN, + MimeTypes.TEXT_VTT, + MimeTypes.TEXT_SSA, + MimeTypes.APPLICATION_TTML, + MimeTypes.APPLICATION_MP4VTT, + MimeTypes.APPLICATION_SUBRIP, + ) ) - ) + } catch (e : Exception) { + logError(e) + } } // Open file picker diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt index 606e1464..a6d9d1f2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt @@ -375,7 +375,11 @@ class SettingsFragment : PreferenceFragmentCompat() { {}) { // Last = custom if (it == dirs.size) { - pathPicker.launch(Uri.EMPTY) + try { + pathPicker.launch(Uri.EMPTY) + } catch (e : Exception) { + logError(e) + } } else { // Sets both visual and actual paths. // key = used path