11package com.profullstack.pdfconverter
22
33import android.annotation.SuppressLint
4+ import android.os.Build
45import android.os.Bundle
6+ import android.util.Log
57import android.webkit.WebResourceError
68import android.webkit.WebResourceRequest
79import android.webkit.WebView
810import android.webkit.WebViewClient
911import android.widget.Toast
12+ import androidx.annotation.RequiresApi
1013import androidx.appcompat.app.AppCompatActivity
11- import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
1214
1315class MainActivity : AppCompatActivity () {
1416 private lateinit var webView: WebView
15- private lateinit var swipeRefreshLayout: SwipeRefreshLayout
16- // URL of the PWA - will be loaded from .env file
17+ // URL of the PWA - hardcoded for simplicity
1718 private lateinit var pwaUrl: String
1819
19-
2020 @SuppressLint(" SetJavaScriptEnabled" )
2121 override fun onCreate (savedInstanceState : Bundle ? ) {
2222 super .onCreate(savedInstanceState)
2323 setContentView(R .layout.activity_main)
2424
25- // Get API base URL from .env file
25+ // Get API base URL from Config
2626 pwaUrl = Config .getApiBaseUrl(this )
27+ Log .d(" PDFConverter" , " Loading URL: $pwaUrl " )
28+
29+ // Verify URL is valid
30+ try {
31+ val url = java.net.URL (pwaUrl)
32+ Log .d(" PDFConverter" , " URL is valid: $pwaUrl (protocol: ${url.protocol} , host: ${url.host} , path: ${url.path} )" )
33+ } catch (e: Exception ) {
34+ Log .e(" PDFConverter" , " URL is invalid: $pwaUrl " , e)
35+ // Fall back to hardcoded URL if the one from Config is invalid
36+ pwaUrl = " https://convert2doc.com"
37+ Log .d(" PDFConverter" , " Falling back to hardcoded URL: $pwaUrl " )
38+ }
2739
2840 // Initialize WebView
2941 webView = findViewById(R .id.webView)
30- swipeRefreshLayout = findViewById(R .id.swipeRefreshLayout)
3142
3243 // Configure WebView settings
3344 webView.settings.apply {
@@ -43,36 +54,63 @@ class MainActivity : AppCompatActivity() {
4354 setGeolocationEnabled(true )
4455 allowFileAccess = true
4556 allowContentAccess = true
46- // Enable caching
47- setAppCacheEnabled( true )
57+ // Modern caching is handled by the browser automatically
58+ cacheMode = android.webkit. WebSettings . LOAD_DEFAULT
4859 }
4960
5061 // Set WebViewClient to handle page navigation
5162 webView.webViewClient = object : WebViewClient () {
63+ override fun onPageStarted (view : WebView ? , url : String? , favicon : android.graphics.Bitmap ? ) {
64+ super .onPageStarted(view, url, favicon)
65+ Log .d(" PDFConverter" , " Page loading started: $url " )
66+ }
67+
5268 override fun onPageFinished (view : WebView ? , url : String? ) {
5369 super .onPageFinished(view, url)
54- // Hide refresh indicator when page is loaded
55- swipeRefreshLayout.isRefreshing = false
70+ Log .d(" PDFConverter" , " Page loading finished: $url " )
5671 }
5772
73+ @RequiresApi(Build .VERSION_CODES .M )
5874 override fun onReceivedError (view : WebView ? , request : WebResourceRequest ? , error : WebResourceError ? ) {
5975 super .onReceivedError(view, request, error)
60- Toast .makeText(this @MainActivity, " Error loading page" , Toast .LENGTH_SHORT ).show()
61- swipeRefreshLayout.isRefreshing = false
76+
77+ val errorMessage = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
78+ " Error: ${error?.errorCode} - ${error?.description} "
79+ } else {
80+ " Error loading page"
81+ }
82+
83+ Log .e(" PDFConverter" , " WebView error: $errorMessage for URL: ${request?.url} " )
84+
85+ // Check if this is a 404 error
86+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M && error?.errorCode == - 2 ) {
87+ Log .e(" PDFConverter" , " 404 Not Found error detected" )
88+
89+ // Try to load the fallback URL
90+ val fallbackUrl = " https://convert2doc.com"
91+ if (request?.url.toString() != fallbackUrl) {
92+ Log .d(" PDFConverter" , " Trying fallback URL: $fallbackUrl " )
93+ view?.loadUrl(fallbackUrl)
94+ Toast .makeText(this @MainActivity, " Page not found, trying fallback URL" , Toast .LENGTH_LONG ).show()
95+ return
96+ }
97+ }
98+
99+ Toast .makeText(this @MainActivity, errorMessage, Toast .LENGTH_LONG ).show()
62100 }
63101
64102 // Keep navigation within the WebView
65103 override fun shouldOverrideUrlLoading (view : WebView ? , request : WebResourceRequest ? ): Boolean {
104+ Log .d(" PDFConverter" , " Navigation to: ${request?.url} " )
66105 return false
67106 }
68107 }
69108
70- // Set up swipe to refresh
71- swipeRefreshLayout.setOnRefreshListener {
72- webView.reload()
73- }
109+ // Check if URL is accessible
110+ checkUrlAccessibility(pwaUrl)
74111
75112 // Load the PWA
113+ Log .d(" PDFConverter" , " Loading PWA URL: $pwaUrl " )
76114 webView.loadUrl(pwaUrl)
77115 }
78116
@@ -84,4 +122,33 @@ class MainActivity : AppCompatActivity() {
84122 super .onBackPressed()
85123 }
86124 }
125+
126+ // Check if URL is accessible
127+ private fun checkUrlAccessibility (url : String ) {
128+ Thread {
129+ try {
130+ Log .d(" PDFConverter" , " Checking URL accessibility: $url " )
131+ val connection = java.net.URL (url).openConnection() as java.net.HttpURLConnection
132+ connection.connectTimeout = 5000
133+ connection.readTimeout = 5000
134+ connection.requestMethod = " HEAD"
135+ val responseCode = connection.responseCode
136+ Log .d(" PDFConverter" , " URL accessibility check result: $url - Response code: $responseCode " )
137+
138+ if (responseCode == 200 ) {
139+ Log .d(" PDFConverter" , " URL is accessible: $url " )
140+ } else {
141+ Log .e(" PDFConverter" , " URL is not accessible: $url - Response code: $responseCode " )
142+ runOnUiThread {
143+ Toast .makeText(this @MainActivity, " Warning: URL may not be accessible (HTTP $responseCode )" , Toast .LENGTH_LONG ).show()
144+ }
145+ }
146+ } catch (e: Exception ) {
147+ Log .e(" PDFConverter" , " Error checking URL accessibility: $url " , e)
148+ runOnUiThread {
149+ Toast .makeText(this @MainActivity, " Warning: Could not check URL accessibility" , Toast .LENGTH_LONG ).show()
150+ }
151+ }
152+ }.start()
153+ }
87154}
0 commit comments