浏览代码

Fixed SSL issue on webview for lower versions, commented code which is not needed

Khubaib 6 月之前
父节点
当前提交
5ec67751f4

+ 2 - 2
.idea/deploymentTargetSelector.xml

@@ -4,10 +4,10 @@
     <selectionStates>
       <SelectionState runConfigName="app">
         <option name="selectionMode" value="DROPDOWN" />
-        <DropdownSelection timestamp="2024-10-28T20:06:37.124240362Z">
+        <DropdownSelection timestamp="2024-10-30T15:51:16.451763817Z">
           <Target type="DEFAULT_BOOT">
             <handle>
-              <DeviceId pluginId="PhysicalDevice" identifier="serial=1C051FDF60048Z" />
+              <DeviceId pluginId="LocalEmulator" identifier="path=/home/ubuntu/.android/avd/Medium_Phone_API_24.avd" />
             </handle>
           </Target>
         </DropdownSelection>

+ 5 - 5
app/src/main/java/com/vpn/fastestvpnservice/screens/LoginScreen.kt

@@ -206,6 +206,7 @@ fun Login(navHostController: NavHostController) {
                         )
 
                         Spacer(modifier = Modifier.height(20.dp))
+
                         TextField(
                             value = textChanged,
                             onValueChange = {
@@ -225,7 +226,8 @@ fun Login(navHostController: NavHostController) {
                                     shape = RoundedCornerShape(16.dp)
                                 )
                                 .background(color = colorResource(id = R.color.transparent))
-                                .clickable() { },
+                            ,
+//                                .clickable() { },
                             shape = RoundedCornerShape(16.dp),
 
 //                placeholder = {l
@@ -269,8 +271,6 @@ fun Login(navHostController: NavHostController) {
                             ),
                             )
 
-
-
                         Spacer(modifier = Modifier.height(20.dp))
 
                         val color = if (showErrorPass1) {
@@ -302,8 +302,8 @@ fun Login(navHostController: NavHostController) {
                                     color = color,
                                     shape = RoundedCornerShape(16.dp)
                                 )
-                                .background(color = colorResource(id = R.color.transparent))
-                                .clickable() {},
+                                .background(color = colorResource(id = R.color.transparent)),
+//                                .clickable() {},
 
                             shape = RoundedCornerShape(16.dp),
 //                placeholder = {

+ 1 - 7
app/src/main/java/com/vpn/fastestvpnservice/screens/helpScreensAll/AboutScreen.kt

@@ -79,13 +79,7 @@ fun About(navHostController: NavHostController) {
             val aboutUrl = if (isDarkTheme.value) "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/about?skin=dark"
             else "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/about?device=ios"
 
-            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
-                Log.d("test_webview", "CustomWebView()")
-                CustomWebView(url = aboutUrl)
-            } else {
-                Log.d("test_webview", "WebView()")
-                WebView(url = aboutUrl)
-            }
+            WebView(url = aboutUrl)
 
 //            AndroidView(factory = {
 //                WebView(it).apply {

+ 79 - 83
app/src/main/java/com/vpn/fastestvpnservice/screens/helpScreensAll/CustomerSupportScreen.kt

@@ -98,11 +98,7 @@ fun CustomerSupport(navHostController: NavHostController, activity: ComponentAct
             window.navigationBarColor = colorResource(id = R.color.white).toArgb()
             WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = true
 
-            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
-                CustomCSWebView(url = supportUrl, activity)
-            } else {
-                CSWebView(url = supportUrl, activity)
-            }
+            CSWebView(url = supportUrl, activity)
 
             if (showLoader.value) {
 
@@ -236,84 +232,84 @@ fun CSWebView(url: String, activity: ComponentActivity) {
     )
 }
 
-@Composable
-fun CustomCSWebView(url: String, activity: ComponentActivity) {
-    AndroidView(factory = {
-        val webView = WebView(it).apply {
-            layoutParams = ViewGroup.LayoutParams(
-                ViewGroup.LayoutParams.MATCH_PARENT,
-                ViewGroup.LayoutParams.MATCH_PARENT
-            )
-            webViewClient = WebViewClient()
-
-            settings.javaScriptEnabled = true
-            settings.allowContentAccess = true
-            settings.allowFileAccess = true
-            settings.allowFileAccessFromFileURLs = true
-            settings.allowUniversalAccessFromFileURLs = true
-            settings.databaseEnabled = true
-            settings.domStorageEnabled = true
-            settings.loadsImagesAutomatically = true
-            settings.builtInZoomControls = true
-            settings.displayZoomControls = true
-            settings.mediaPlaybackRequiresUserGesture = false
-            settings.supportZoom()
-            settings.loadWithOverviewMode = true
-            setLayerType(View.LAYER_TYPE_NONE, null)
-            settings.databaseEnabled = true
-
-            webViewClient = object : WebViewClient() {
-                override fun onReceivedSslError(
-                    view: WebView?,
-                    handler: SslErrorHandler?,
-                    error: SslError?
-                ) {
-                    handler?.proceed()
-                }
-            }
-
-            webChromeClient = object : WebChromeClient() {
-                override fun onPermissionRequest(request: PermissionRequest?) {
-                    super.onPermissionRequest(request)
-                    Log.d("test_file_chat", "onActivityResult: onPermissionRequest")
-                    request?.grant(request.resources)
-                }
-
-                override fun onShowFileChooser(
-                    webView: WebView?,
-                    filePathCallback: ValueCallback<Array<Uri>>?,
-                    fileChooserParams: FileChooserParams?
-                ): Boolean {
-                    Log.d("test_file_chat", "onActivityResult: onShowFileChooser")
-                    fileChooserCallback?.onReceiveValue(null)
-                    fileChooserCallback = filePathCallback
-                    val selectionIntent = Intent(Intent.ACTION_GET_CONTENT)
-                    selectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
-                    selectionIntent.type = "*/*"
-                    val chooserIntent = Intent(Intent.ACTION_CHOOSER)
-                    chooserIntent.putExtra(Intent.EXTRA_INTENT, selectionIntent)
-                    activity.startActivityForResult(chooserIntent, 551)
-
-                    return true
-                }
-
-            }
-
-            setDownloadListener { uri, userAgent, contentDisposition, mimetype, contentLength ->
-                handleURI(
-                    uri
-                )
-            }
-            setOnLongClickListener { v ->
-                handleURI((v as WebView).hitTestResult.extra)
-                true
-            }
-        }
-
-        loadContentWithOkHttp(webView, url)
-        webView
-        })
-}
+//@Composable
+//fun CustomCSWebView(url: String, activity: ComponentActivity) {
+//    AndroidView(factory = {
+//        val webView = WebView(it).apply {
+//            layoutParams = ViewGroup.LayoutParams(
+//                ViewGroup.LayoutParams.MATCH_PARENT,
+//                ViewGroup.LayoutParams.MATCH_PARENT
+//            )
+//            webViewClient = WebViewClient()
+//
+//            settings.javaScriptEnabled = true
+//            settings.allowContentAccess = true
+//            settings.allowFileAccess = true
+//            settings.allowFileAccessFromFileURLs = true
+//            settings.allowUniversalAccessFromFileURLs = true
+//            settings.databaseEnabled = true
+//            settings.domStorageEnabled = true
+//            settings.loadsImagesAutomatically = true
+//            settings.builtInZoomControls = true
+//            settings.displayZoomControls = true
+//            settings.mediaPlaybackRequiresUserGesture = false
+//            settings.supportZoom()
+//            settings.loadWithOverviewMode = true
+//            setLayerType(View.LAYER_TYPE_NONE, null)
+//            settings.databaseEnabled = true
+//
+//            webViewClient = object : WebViewClient() {
+//                override fun onReceivedSslError(
+//                    view: WebView?,
+//                    handler: SslErrorHandler?,
+//                    error: SslError?
+//                ) {
+//                    handler?.proceed()
+//                }
+//            }
+//
+//            webChromeClient = object : WebChromeClient() {
+//                override fun onPermissionRequest(request: PermissionRequest?) {
+//                    super.onPermissionRequest(request)
+//                    Log.d("test_file_chat", "onActivityResult: onPermissionRequest")
+//                    request?.grant(request.resources)
+//                }
+//
+//                override fun onShowFileChooser(
+//                    webView: WebView?,
+//                    filePathCallback: ValueCallback<Array<Uri>>?,
+//                    fileChooserParams: FileChooserParams?
+//                ): Boolean {
+//                    Log.d("test_file_chat", "onActivityResult: onShowFileChooser")
+//                    fileChooserCallback?.onReceiveValue(null)
+//                    fileChooserCallback = filePathCallback
+//                    val selectionIntent = Intent(Intent.ACTION_GET_CONTENT)
+//                    selectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
+//                    selectionIntent.type = "*/*"
+//                    val chooserIntent = Intent(Intent.ACTION_CHOOSER)
+//                    chooserIntent.putExtra(Intent.EXTRA_INTENT, selectionIntent)
+//                    activity.startActivityForResult(chooserIntent, 551)
+//
+//                    return true
+//                }
+//
+//            }
+//
+//            setDownloadListener { uri, userAgent, contentDisposition, mimetype, contentLength ->
+//                handleURI(
+//                    uri
+//                )
+//            }
+//            setOnLongClickListener { v ->
+//                handleURI((v as WebView).hitTestResult.extra)
+//                true
+//            }
+//        }
+//
+//        loadContentWithOkHttp(webView, url)
+//        webView
+//        })
+//}
 
 fun handleURI(uri: String?) {
     if (uri != null) {

+ 110 - 116
app/src/main/java/com/vpn/fastestvpnservice/screens/helpScreensAll/FAQScreen.kt

@@ -114,13 +114,7 @@ fun FAQ(navHostController: NavHostController) {
             val faqUrl = if (isDarkTheme.value) "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/faq?skin=dark"
             else "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/faq?device=ios"
 
-            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
-                Log.d("test_webview", "CustomWebView()")
-                CustomWebView(url = faqUrl)
-            } else {
-                Log.d("test_webview", "WebView()")
-                WebView(url = faqUrl)
-            }
+            WebView(url = faqUrl)
         }
 
         if (showLoader.value) {
@@ -150,38 +144,38 @@ fun FAQ(navHostController: NavHostController) {
     }
 }
 
-@Composable
-fun BoxScope.CustomWebView(url: String) {
-    AndroidView(factory = { context ->
-        val webView = WebView(context).apply {
-            layoutParams = ViewGroup.LayoutParams(
-                ViewGroup.LayoutParams.MATCH_PARENT,
-                ViewGroup.LayoutParams.MATCH_PARENT
-            )
-
-            settings.apply {
-                javaScriptEnabled = true
-                domStorageEnabled = true
-                loadsImagesAutomatically = true
-                mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
-            }
-
-            webViewClient = object : WebViewClient() {
-                override fun onReceivedSslError(
-                    view: WebView?,
-                    handler: android.webkit.SslErrorHandler?,
-                    error: android.net.http.SslError?
-                ) {
-                    handler?.proceed() // Bypass SSL certificate issues (for debugging)
-                }
-            }
-        }
-
-        // Make the OkHttp request
-        loadContentWithOkHttp(webView, url)
-        webView
-    })
-}
+//@Composable
+//fun BoxScope.CustomWebView(url: String) {
+//    AndroidView(factory = { context ->
+//        val webView = WebView(context).apply {
+//            layoutParams = ViewGroup.LayoutParams(
+//                ViewGroup.LayoutParams.MATCH_PARENT,
+//                ViewGroup.LayoutParams.MATCH_PARENT
+//            )
+//
+//            settings.apply {
+//                javaScriptEnabled = true
+//                domStorageEnabled = true
+//                loadsImagesAutomatically = true
+//                mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
+//            }
+//
+//            webViewClient = object : WebViewClient() {
+//                override fun onReceivedSslError(
+//                    view: WebView?,
+//                    handler: android.webkit.SslErrorHandler?,
+//                    error: android.net.http.SslError?
+//                ) {
+//                    handler?.proceed() // Bypass SSL certificate issues (for debugging)
+//                }
+//            }
+//        }
+//
+//        // Make the OkHttp request
+//        loadContentWithOkHttp(webView, url)
+//        webView
+//    })
+//}
 
 @Composable
 fun BoxScope.WebView(url: String) {
@@ -220,83 +214,83 @@ fun BoxScope.WebView(url: String) {
     )
 }
 
-fun loadContentWithOkHttp(webView: WebView, url: String) {
-    val client = createUnsafeOkHttpClient()
-    val request = Request.Builder().url(url).build()
-
-    Log.d("test_webview", "loadContentWithOkHttp")
-
-    client.newCall(request).enqueue(object : okhttp3.Callback {
-        override fun onFailure(call: okhttp3.Call, e: IOException) {
-            showLoader.value = false
-            Log.d("test_webview", "onFailure")
-            Log.e("CustomWebView", "Failed to load URL with OkHttp: ${e.message}")
-        }
-
-        override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
-            showLoader.value = false
-            Log.d("test_webview", "onResponse")
-
-            val htmlContent = response.body?.string() ?: ""
-            // Load the HTML content into the WebView on the main thread
-
-            webView.post {
-                showLoader.value = false
-                Log.d("test_webview", "webView.post")
-                webView.loadDataWithBaseURL(url, htmlContent, "text/html", "UTF-8", null)
-            }
-        }
-    })
-}
-
-
-fun WebView.trustAllCertificates() {
-    try {
-        Log.d("test_webview", "try")
-        val trustAllCerts = arrayOf<TrustManager>(
-            object : X509TrustManager {
-                override fun checkClientTrusted(
-                    chain: Array<out X509Certificate>?,
-                    authType: String?
-                ) {}
-
-                override fun checkServerTrusted(
-                    chain: Array<out X509Certificate>?,
-                    authType: String?
-                ) {}
-
-                override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
-            }
-        )
-
-        val sslContext = SSLContext.getInstance("TLS")
-        sslContext.init(null, trustAllCerts, SecureRandom())
-        this.settings.javaScriptEnabled = true
-        // Set the SSL factory to the custom context that trusts all certificates
-        sslContext.socketFactory
-    } catch (e: Exception) {
-        Log.d("test_webview", "catch")
-    }
-}
-
-fun createUnsafeOkHttpClient(): OkHttpClient {
-    val trustAllCerts = arrayOf<TrustManager>(
-        object : X509TrustManager {
-            override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
-            override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
-            override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
-        }
-    )
-
-    val sslContext = SSLContext.getInstance("TLS")
-    sslContext.init(null, trustAllCerts, SecureRandom())
-    val sslSocketFactory = sslContext.socketFactory
-
-    return OkHttpClient.Builder()
-        .sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
-        .hostnameVerifier(HostnameVerifier { _, _ -> true })
-        .build()
-}
+//fun loadContentWithOkHttp(webView: WebView, url: String) {
+//    val client = createUnsafeOkHttpClient()
+//    val request = Request.Builder().url(url).build()
+//
+//    Log.d("test_webview", "loadContentWithOkHttp")
+//
+//    client.newCall(request).enqueue(object : okhttp3.Callback {
+//        override fun onFailure(call: okhttp3.Call, e: IOException) {
+//            showLoader.value = false
+//            Log.d("test_webview", "onFailure")
+//            Log.e("CustomWebView", "Failed to load URL with OkHttp: ${e.message}")
+//        }
+//
+//        override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
+//            showLoader.value = false
+//            Log.d("test_webview", "onResponse")
+//
+//            val htmlContent = response.body?.string() ?: ""
+//            // Load the HTML content into the WebView on the main thread
+//
+//            webView.post {
+//                showLoader.value = false
+//                Log.d("test_webview", "webView.post")
+//                webView.loadDataWithBaseURL(url, htmlContent, "text/html", "UTF-8", null)
+//            }
+//        }
+//    })
+//}
+
+
+//fun WebView.trustAllCertificates() {
+//    try {
+//        Log.d("test_webview", "try")
+//        val trustAllCerts = arrayOf<TrustManager>(
+//            object : X509TrustManager {
+//                override fun checkClientTrusted(
+//                    chain: Array<out X509Certificate>?,
+//                    authType: String?
+//                ) {}
+//
+//                override fun checkServerTrusted(
+//                    chain: Array<out X509Certificate>?,
+//                    authType: String?
+//                ) {}
+//
+//                override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
+//            }
+//        )
+//
+//        val sslContext = SSLContext.getInstance("TLS")
+//        sslContext.init(null, trustAllCerts, SecureRandom())
+//        this.settings.javaScriptEnabled = true
+//        // Set the SSL factory to the custom context that trusts all certificates
+//        sslContext.socketFactory
+//    } catch (e: Exception) {
+//        Log.d("test_webview", "catch")
+//    }
+//}
+//
+//fun createUnsafeOkHttpClient(): OkHttpClient {
+//    val trustAllCerts = arrayOf<TrustManager>(
+//        object : X509TrustManager {
+//            override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
+//            override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
+//            override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
+//        }
+//    )
+//
+//    val sslContext = SSLContext.getInstance("TLS")
+//    sslContext.init(null, trustAllCerts, SecureRandom())
+//    val sslSocketFactory = sslContext.socketFactory
+//
+//    return OkHttpClient.Builder()
+//        .sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
+//        .hostnameVerifier(HostnameVerifier { _, _ -> true })
+//        .build()
+//}
 
 @Composable
 fun BoxScope.ShowExpandList(

+ 1 - 7
app/src/main/java/com/vpn/fastestvpnservice/screens/helpScreensAll/PrivacyPolicyScreen.kt

@@ -94,13 +94,7 @@ fun PrivacyPolicy(navHostController: NavHostController){
             val privacyUrl = if (isDarkTheme.value) "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/privacy-policy?skin=dark"
             else "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/privacy-policy?device=ios"
 
-            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
-                Log.d("test_webview", "CustomWebView()")
-                CustomWebView(url = privacyUrl)
-            } else {
-                Log.d("test_webview", "WebView()")
-                WebView(url = privacyUrl)
-            }
+            WebView(url = privacyUrl)
 
 //            AndroidView(factory = {
 //                WebView(it).apply {

+ 1 - 7
app/src/main/java/com/vpn/fastestvpnservice/screens/helpScreensAll/TermsAndConditionsScreen.kt

@@ -101,13 +101,7 @@ fun TermsAndConditions(
             val tncUrl = if (isDarkTheme.value) "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/terms-of-service?skin=dark"
             else "${AppConstant.BASE_WEBVIEW_URL}${languageIdentifier}/terms-of-service?device=ios"
 
-            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
-                Log.d("test_webview", "CustomWebView()")
-                CustomWebView(url = tncUrl)
-            } else {
-                Log.d("test_webview", "WebView()")
-                WebView(url = tncUrl)
-            }
+            WebView(url = tncUrl)
 
 //            AndroidView(factory = {
 //                WebView(it).apply {