Skip to content

Commit

Permalink
[WebView Bug. http/https shcema URLs] due to Chromium bug 'crbug.com/…
Browse files Browse the repository at this point in the history
…1244039', Sometimes WebView's onPageFinished method won't be called
  • Loading branch information
mecoFarid committed Aug 27, 2021
1 parent 955805e commit 1913fb3
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 274 deletions.
42 changes: 19 additions & 23 deletions app/src/main/java/com/mecofarid/pdfkitdemo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mecofarid.pdfkitdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mecofarid.pdfkit.PdfKit
import java.io.File

Expand All @@ -10,40 +10,36 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Thread()
// Handler(HandlerThread("PdfConverterHandlerThread").looper) {
// getFile("PdfKitDemo_1.pdf").let { outputFile ->
// PdfKit(application.applicationContext).startConversion(
// url = "https://stackoverflow.com/",
// outputFile = outputFile,
// onPdfPrintListener = object : PdfKit.OnPdfConversionListener {
// override fun onError(e: Exception) {
// println("PDFPRINT onError: $e")
// }
//
// override fun onSuccess(pdfFileLocation: File) {
// println("PDFPRINT onSuccess: $outputFile")
// }
// }
// )
// }
// }.start()


getFile("PdfKitDemo_1.pdf").let { outputFile ->
PdfKit(application.applicationContext).startConversion(
url = "https://www.github.com/",
outputFile = outputFile,
javascriptEnabled = true,
onPdfPrintListener = object : PdfKit.OnPdfConversionListener {
override fun onError(e: Exception) {
println("PDFPRINT remote onError: $e")
}

override fun onSuccess(pdfFileLocation: File) {
println("PDFPRINT remote onSuccess: $outputFile")
}
}
)
}

getFile("PdfKitDemo_2.pdf").let { outputFile ->
PdfKit(this).startConversion(
baseUrl = getAssetDirectoryPath(),
data = PdfConverterDataFactory.htmlData,
javascriptEnabled = true,
outputFile = outputFile,
onPdfPrintListener = object : PdfKit.OnPdfConversionListener {
override fun onError(e: Exception) {
println("PDFPRINT onError: $e")
println("PDFPRINT local onError: $e")
}

override fun onSuccess(pdfFileLocation: File) {
println("PDFPRINT onSuccess: $outputFile")
println("PDFPRINT local onSuccess: $outputFile")
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object PdfConverterDataFactory {
"</head>\n" +
"<body>\n" +
"\n" +
"<img src=\"../random.jpg\" alt=\"Random image\">\n" +
"<img src=\"random.jpg\" alt=\"Random image\">\n" +
"\n" +
"<h2>Striped Table</h2>\n" +
"<p>For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:</p>\n" +
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.mecofarid.pdfkit

import android.content.Context
import android.os.Handler
import android.os.HandlerThread
import android.os.Message
import android.util.Log
import android.webkit.WebView
import androidx.test.core.app.ApplicationProvider
Expand All @@ -13,6 +15,9 @@ import org.junit.runner.RunWith
import org.junit.Assert.*
import org.junit.BeforeClass
import java.io.File
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

/**
* Instrumented test, which will execute on an Android device.
Expand All @@ -32,21 +37,19 @@ class PdfKitAndroidTest {
fun test_startConversion_1() {
getExternalFilesDir("PdfKitDemo_1.pdf")?.let { outputFile ->
println(println("Logger a-00"))
Handler(getContext().mainLooper).post {
PdfKit(getContext()).startConversion(
url = "https://stackoverflow.com/",
outputFile = outputFile,
onPdfPrintListener = object : PdfKit.OnPdfConversionListener {
override fun onError(e: Exception) {
println("PDFPRINT onError: $e")
}
PdfKit(getContext()).startConversion(
url = "https://stackoverflow.com/",
outputFile = outputFile,
onPdfPrintListener = object : PdfKit.OnPdfConversionListener {
override fun onError(e: Exception) {
println("PDFPRINT onError: $e")
}

override fun onSuccess(pdfFileLocation: File) {
println("PDFPRINT onSuccess: $outputFile")
}
override fun onSuccess(pdfFileLocation: File) {
println("PDFPRINT onSuccess: $outputFile")
}
)
}
}
)
}
}

Expand Down Expand Up @@ -74,25 +77,6 @@ class PdfKitAndroidTest {

}

@Test
fun test_startConversion_3() {
getExternalFilesDir("PdfKitDemo_3.pdf")?.let {outputFile->
PdfKit(getContext()).startConversion(
webView = WebView(getContext()),
outputFile = outputFile,
onPdfPrintListener = object : PdfKit.OnPdfConversionListener {
override fun onError(e: Exception) {
Log.d("TAG", "onError: $e")
}

override fun onSuccess(pdfFileLocation: File) {
assertEquals(pdfFileLocation, outputFile)
}
}
)
}
}

private fun getExternalFilesDir(fileName: String): File{
return File(getContext().getExternalFilesDir("pdf"), fileName).apply {
createNewFile()
Expand All @@ -101,4 +85,5 @@ class PdfKitAndroidTest {

private fun getContext(): Context =
ApplicationProvider.getApplicationContext()

}
4 changes: 0 additions & 4 deletions pdfkit/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mecofarid.pdfkit">
<uses-permission android:name="android.permission.INTERNET"/>
<application
tools:targetApi="m"
android:usesCleartextTraffic="true"/>
</manifest>
32 changes: 32 additions & 0 deletions pdfkit/src/main/java/android/print/InternalConversionHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package android.print

import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.os.Message
import android.util.Log
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadFactory
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

internal object InternalConversionHandler{

private const val CORE_POOL_SIZE = 5
private const val MAXIMUM_POOL_SIZE = 20
private const val KEEP_ALIVE_SECONDS = 3L

private val threadPoolExecutor = ThreadPoolExecutor(
CORE_POOL_SIZE,
MAXIMUM_POOL_SIZE,
KEEP_ALIVE_SECONDS,
TimeUnit.SECONDS,
LinkedBlockingQueue()
).also {
it.allowCoreThreadTimeOut(true)
}

fun execute(runnable: Runnable){
threadPoolExecutor.execute(runnable)
}
}
Loading

0 comments on commit 1913fb3

Please sign in to comment.