@@ -38,15 +38,13 @@ import androidx.lifecycle.lifecycleScope
3838import com.outsystems.plugins.inappbrowser.osinappbrowserlib.OSIABEvents
3939import com.outsystems.plugins.inappbrowser.osinappbrowserlib.OSIABEvents.OSIABWebViewEvent
4040import com.outsystems.plugins.inappbrowser.osinappbrowserlib.R
41+ import com.outsystems.plugins.inappbrowser.osinappbrowserlib.helpers.OSIABPdfHelper
4142import com.outsystems.plugins.inappbrowser.osinappbrowserlib.models.OSIABToolbarPosition
4243import com.outsystems.plugins.inappbrowser.osinappbrowserlib.models.OSIABWebViewOptions
4344import kotlinx.coroutines.Dispatchers
4445import kotlinx.coroutines.launch
4546import kotlinx.coroutines.withContext
46- import java.io.File
4747import java.io.IOException
48- import java.net.HttpURLConnection
49- import java.net.URL
5048
5149class OSIABWebViewActivity : AppCompatActivity () {
5250
@@ -219,8 +217,8 @@ class OSIABWebViewActivity : AppCompatActivity() {
219217
220218 private fun handleLoadUrl (url : String , additionalHttpHeaders : Map <String , String >? = null) {
221219 lifecycleScope.launch(Dispatchers .IO ) {
222- if (isContentTypeApplicationPdf(url)) {
223- val pdfFile = try { downloadPdfToCache(url) } catch (_: IOException ) { null }
220+ if (OSIABPdfHelper . isContentTypeApplicationPdf(url)) {
221+ val pdfFile = try { OSIABPdfHelper . downloadPdfToCache(this @OSIABWebViewActivity, url) } catch (_: IOException ) { null }
224222 if (pdfFile != null ) {
225223 withContext(Dispatchers .Main ) {
226224 webView.stopLoading()
@@ -239,51 +237,6 @@ class OSIABWebViewActivity : AppCompatActivity() {
239237 }
240238 }
241239
242- fun isContentTypeApplicationPdf (urlString : String ): Boolean {
243- return try {
244- // Try to identify if the URL is a PDF using a HEAD request.
245- // If the server does not implement HEAD correctly or does not return the expected content-type,
246- // fall back to a GET request, since some servers only return the correct type for GET.
247- if (checkPdfByRequest(urlString, method = " HEAD" )) {
248- true
249- } else {
250- checkPdfByRequest(urlString, method = " GET" )
251- }
252- } catch (_: Exception ) {
253- false
254- }
255- }
256-
257- private fun checkPdfByRequest (urlString : String , method : String ): Boolean {
258- var conn: HttpURLConnection ? = null
259- return try {
260- conn = (URL (urlString).openConnection() as ? HttpURLConnection )
261- conn?.run {
262- instanceFollowRedirects = true
263- requestMethod = method
264- if (method == " GET" ) {
265- setRequestProperty(" Range" , " bytes=0-0" )
266- }
267- connect()
268- val type = contentType?.lowercase()
269- val disposition = getHeaderField(" Content-Disposition" )?.lowercase()
270- type == " application/pdf" ||
271- (type.isNullOrEmpty() && disposition?.contains(" .pdf" ) == true )
272- } ? : false
273- } finally {
274- conn?.disconnect()
275- }
276- }
277-
278- private fun downloadPdfToCache (url : String ): File {
279- val pdfFile = File (cacheDir, " temp_${System .currentTimeMillis()} .pdf" )
280- URL (url).openStream().use { input ->
281- pdfFile.outputStream().use { output ->
282- input.copyTo(output)
283- }
284- }
285- return pdfFile
286- }
287240
288241 /* *
289242 * Helper function to update navigation button states
@@ -418,6 +371,15 @@ class OSIABWebViewActivity : AppCompatActivity() {
418371 sendWebViewEvent(OSIABEvents .BrowserPageNavigationCompleted (browserId, resolvedUrl))
419372 }
420373
374+ if (url?.startsWith(PDF_VIEWER_URL_PREFIX ) == true ) {
375+ if (options.clearCache) {
376+ webView.evaluateJavascript(
377+ " localStorage.clear(); sessionStorage.clear();" , null
378+ );
379+ }
380+ }
381+
382+
421383 // set back to false so that the next successful load
422384 // if the load fails, onReceivedError takes care of setting it back to true
423385 hasLoadError = false
0 commit comments