diff --git a/app/src/main/java/com/mecofarid/pdfkitdemo/MainActivity.kt b/app/src/main/java/com/mecofarid/pdfkitdemo/MainActivity.kt index 2aa5745..cf30c6e 100644 --- a/app/src/main/java/com/mecofarid/pdfkitdemo/MainActivity.kt +++ b/app/src/main/java/com/mecofarid/pdfkitdemo/MainActivity.kt @@ -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 @@ -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") } } ) diff --git a/app/src/main/java/com/mecofarid/pdfkitdemo/PdfConverterDataFactory.kt b/app/src/main/java/com/mecofarid/pdfkitdemo/PdfConverterDataFactory.kt index c8b6c07..2509b50 100644 --- a/app/src/main/java/com/mecofarid/pdfkitdemo/PdfConverterDataFactory.kt +++ b/app/src/main/java/com/mecofarid/pdfkitdemo/PdfConverterDataFactory.kt @@ -20,7 +20,7 @@ object PdfConverterDataFactory { "\n" + "\n" + "\n" + - "\"Random\n" + + "\"Random\n" + "\n" + "

Striped Table

\n" + "

For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:

\n" + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index bb16909..be7e075 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,13 +6,13 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + \ No newline at end of file diff --git a/pdfkit/src/androidTest/java/com/mecofarid/pdfkit/PdfKitAndroidTest.kt b/pdfkit/src/androidTest/java/com/mecofarid/pdfkit/PdfKitAndroidTest.kt index cc5d1c4..561cc31 100644 --- a/pdfkit/src/androidTest/java/com/mecofarid/pdfkit/PdfKitAndroidTest.kt +++ b/pdfkit/src/androidTest/java/com/mecofarid/pdfkit/PdfKitAndroidTest.kt @@ -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 @@ -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. @@ -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") } - ) - } + } + ) } } @@ -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() @@ -101,4 +85,5 @@ class PdfKitAndroidTest { private fun getContext(): Context = ApplicationProvider.getApplicationContext() + } \ No newline at end of file diff --git a/pdfkit/src/main/AndroidManifest.xml b/pdfkit/src/main/AndroidManifest.xml index 7d5a40c..b3f0314 100644 --- a/pdfkit/src/main/AndroidManifest.xml +++ b/pdfkit/src/main/AndroidManifest.xml @@ -1,9 +1,5 @@ - \ No newline at end of file diff --git a/pdfkit/src/main/java/android/print/InternalConversionHandler.kt b/pdfkit/src/main/java/android/print/InternalConversionHandler.kt new file mode 100644 index 0000000..94853b6 --- /dev/null +++ b/pdfkit/src/main/java/android/print/InternalConversionHandler.kt @@ -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) + } +} \ No newline at end of file diff --git a/pdfkit/src/main/java/android/print/InternalPdfConverter.kt b/pdfkit/src/main/java/android/print/InternalPdfConverter.kt new file mode 100644 index 0000000..2f45236 --- /dev/null +++ b/pdfkit/src/main/java/android/print/InternalPdfConverter.kt @@ -0,0 +1,185 @@ +package android.print + +import android.net.http.SslError +import android.os.Build +import android.os.ParcelFileDescriptor +import android.util.EventLogTags +import android.webkit.* +import com.mecofarid.logger.Logger +import com.mecofarid.pdfkit.PdfKit +import java.io.File + +private const val ERROR_WEBVIEW_LAYOUT_CANCELLED = "Error webview layout cancelled" +private const val ERROR_WEBVIEW_WRITE_CANCELLED = "Error webview write cancelled" +private const val ABOUT_BLANK_DISPLAY_URL = "about:blank" +private const val TAG = "InternalPdfConverter"; +internal object InternalPdfConverter { + private fun startConversionInternal(webView: WebView, printAttributes: PrintAttributes, javascriptEnabled: Boolean, onPdfPrintListener: PdfKit.OnPdfConversionListener, outputFile: File) { + fun shouldByPassError(webView: WebView):Boolean = ABOUT_BLANK_DISPLAY_URL.equals(webView.url) + + Logger.d(TAG, "startConversionInternal: ") + webView.clearCache(true) + webView.clearHistory() + webView.settings.javaScriptEnabled = javascriptEnabled + webView.webViewClient = object : WebViewClient() { + override fun onPageFinished(view: WebView, url: String?) { + Logger.d(TAG, "onPageFinished: ") + super.onPageFinished(view, url) + val printDocumentAdapter = getPrintAdapter(webView = webView) + + // Execute conversion on non-UI thread + InternalConversionHandler.execute { + printDocumentAdapter.onLayout( + null, + printAttributes, + null, + object : PrintDocumentAdapter.LayoutResultCallback() { + override fun onLayoutCancelled() { + Logger.d(TAG, "onLayoutCancelled: ") + super.onLayoutCancelled() + onPdfPrintListener.onError( + RuntimeException( + ERROR_WEBVIEW_LAYOUT_CANCELLED + ) + ) + } + + override fun onLayoutFailed(error: CharSequence?) { + Logger.d(TAG, "onLayoutFailed: $error") + super.onLayoutFailed(error) + onPdfPrintListener.onError(RuntimeException(error.toString())) + } + + override fun onLayoutFinished(info: PrintDocumentInfo, changed: Boolean) { + Logger.d(TAG, "onLayoutFinished: ") + val fileDescriptorResult = getFileDescriptorResult(file = outputFile) + if (!fileDescriptorResult.isSuccess()) { + onPdfPrintListener.onError(fileDescriptorResult.getFailureData()) + return + } + + printDocumentAdapter.onWrite( + arrayOf(PageRange.ALL_PAGES), + fileDescriptorResult.getSuccessData(), + null, + object : PrintDocumentAdapter.WriteResultCallback() { + override fun onWriteCancelled() { + Logger.d(TAG, "onWriteCancelled: ") + super.onWriteCancelled() + onPdfPrintListener.onError( + RuntimeException( + ERROR_WEBVIEW_WRITE_CANCELLED + ) + ) + } + + override fun onWriteFailed(error: CharSequence) { + Logger.d(TAG, "onWriteFailed: $error") + super.onWriteFailed(error) + onPdfPrintListener.onError(RuntimeException(error.toString())) + } + + override fun onWriteFinished(pages: Array) { + Logger.d(TAG, "onWriteFinished: ") + super.onWriteFinished(pages) + onPdfPrintListener.onSuccess(outputFile) + } + }) + } + }, + null + ) + } + } + + override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) { + super.onReceivedError(view, request, error) + + // Ignore error while loading blank page. This happens when loadDataWithBaseURL provides null for + // historyUrl + if (shouldByPassError(webView)){ + return + } + + var errorString = "Error loading ${webView.url}; " + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M){ + errorString += "Error code: ${error.errorCode}; Description: ${error.description}; " + } + + onPdfPrintListener.onError(Exception(errorString)) + } + + override fun onReceivedHttpError(view: WebView, request: WebResourceRequest, errorResponse: WebResourceResponse) { + super.onReceivedHttpError(view, request, errorResponse) + + // Ignore error while loading blank page. This happens when loadDataWithBaseURL provides null for + // historyUrl + if (shouldByPassError(webView)){ + return + } + + var errorString = "Error loading ${webView.url}; " + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M){ + errorString += "HTTP Status code: ${errorResponse.statusCode}; Description: ${errorResponse.reasonPhrase}; " + } + + onPdfPrintListener.onError(Exception(errorString)) + } + + override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) { + super.onReceivedSslError(view, handler, error) + + // Ignore error while loading blank page. This happens when loadDataWithBaseURL provides null for + // historyUrl + if (shouldByPassError(webView)){ + return + } + + val errorString = "Error loading ${webView.url}; $error" + onPdfPrintListener.onError(Exception(errorString)) + } + } + } + + fun startConversion( + webView: WebView, + printAttributes: PrintAttributes?, + javascriptEnabled: Boolean, + outputFile: File, + onPdfPrintListener: PdfKit.OnPdfConversionListener + ) { + val nonNullPrintAttributes = printAttributes ?: PrintAttributes.Builder() + .setResolution(PrintAttributes.Resolution("RESOLUTION_ID", "RESOLUTION_LABEL", 600, 848)) + .setMinMargins(PrintAttributes.Margins.NO_MARGINS) + .setMediaSize(PrintAttributes.MediaSize.ISO_A4) + .build() + + startConversionInternal(webView, nonNullPrintAttributes, javascriptEnabled,onPdfPrintListener, outputFile) + } + + private fun getFileDescriptorResult(file: File): FileDescriptorResult { + var fileDescriptor: ParcelFileDescriptor? = null + var exception: Exception? = null + try { + file.createNewFile() + fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_TRUNCATE or ParcelFileDescriptor.MODE_READ_WRITE) + } catch (e: Exception) { + + exception = e + } + return FileDescriptorResult(fileDescriptor, exception) + } + + private class FileDescriptorResult( + private val fileDescriptor: ParcelFileDescriptor?, + private val exception: Exception? + ){ + fun isSuccess() = fileDescriptor != null + fun getSuccessData(): ParcelFileDescriptor = fileDescriptor!! + fun getFailureData(): Exception = exception!! + } + + private fun getPrintAdapter(webView: WebView): PrintDocumentAdapter = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) webView.createPrintDocumentAdapter("Document") + else webView.createPrintDocumentAdapter() +} \ No newline at end of file diff --git a/pdfkit/src/main/java/android/print/PdfConverter.kt b/pdfkit/src/main/java/android/print/PdfConverter.kt deleted file mode 100644 index b59354a..0000000 --- a/pdfkit/src/main/java/android/print/PdfConverter.kt +++ /dev/null @@ -1,131 +0,0 @@ -package android.print - -import android.os.Build -import android.os.HandlerThread -import android.os.ParcelFileDescriptor -import android.webkit.WebView -import android.webkit.WebViewClient -import com.mecofarid.logger.Logger -import com.mecofarid.pdfkit.PdfKit -import com.mecofarid.pdfkit.ConversionHandler -import java.io.File - -private const val ERROR_WEBVIEW_LAYOUT_CANCELLED = "Error webview layout cancelled" -private const val ERROR_WEBVIEW_WRITE_CANCELLED = "Error webview write cancelled" -private const val TAG = "InternalPdfConverter"; -internal object InternalPdfConverter { - private fun startConversionInternal(webView: WebView, printAttributes: PrintAttributes, onPdfPrintListener: PdfKit.OnPdfConversionListener, outputFile: File) { - Logger.d(TAG, "startConversionInternal: ") - webView.clearCache(true) - webView.clearHistory() - webView.webViewClient = object : WebViewClient() { - override fun onPageFinished(view: WebView, url: String?) { - Logger.d(TAG, "onPageFinished: ") - super.onPageFinished(view, url) - val printDocumentAdapter = getPrintAdapter(webView = webView) - printDocumentAdapter.onLayout( - null, - printAttributes, - null, - object : PrintDocumentAdapter.LayoutResultCallback() { - override fun onLayoutCancelled() { - Logger.d(TAG, "onLayoutCancelled: ") - super.onLayoutCancelled() - onPdfPrintListener.onError( - RuntimeException( - ERROR_WEBVIEW_LAYOUT_CANCELLED - ) - ) - } - - override fun onLayoutFailed(error: CharSequence?) { - Logger.d(TAG, "onLayoutFailed: $error") - super.onLayoutFailed(error) - onPdfPrintListener.onError(RuntimeException(error.toString())) - } - - override fun onLayoutFinished(info: PrintDocumentInfo, changed: Boolean) { - Logger.d(TAG, "onLayoutFinished: ") - val fileDescriptorResult = getFileDescriptorResult(file = outputFile) - if (!fileDescriptorResult.isSuccess()) { - onPdfPrintListener.onError(fileDescriptorResult.getFailureData()) - return - } - - printDocumentAdapter.onWrite( - arrayOf(PageRange.ALL_PAGES), - fileDescriptorResult.getSuccessData(), - null, - object : PrintDocumentAdapter.WriteResultCallback() { - override fun onWriteCancelled() { - Logger.d(TAG, "onWriteCancelled: ") - super.onWriteCancelled() - onPdfPrintListener.onError( - RuntimeException( - ERROR_WEBVIEW_WRITE_CANCELLED - ) - ) - } - - override fun onWriteFailed(error: CharSequence) { - Logger.d(TAG, "onWriteFailed: $error") - super.onWriteFailed(error) - onPdfPrintListener.onError(RuntimeException(error.toString())) - } - - override fun onWriteFinished(pages: Array) { - Logger.d(TAG, "onWriteFinished: ") - super.onWriteFinished(pages) - onPdfPrintListener.onSuccess(outputFile) - } - }) - } - }, - null - ) - } - } - } - - fun startConversion( - webView: WebView, - printAttributes: PrintAttributes?, - outputFile: File, - onPdfPrintListener: PdfKit.OnPdfConversionListener - ) { - val nonNullPrintAttributes = printAttributes ?: PrintAttributes.Builder() - .setResolution(PrintAttributes.Resolution("RESOLUTION_ID", "RESOLUTION_LABEL", 600, 848)) - .setMinMargins(PrintAttributes.Margins.NO_MARGINS) - .setMediaSize(PrintAttributes.MediaSize.ISO_A4) - .build() - - startConversionInternal(webView, nonNullPrintAttributes, onPdfPrintListener, outputFile) - } - - private fun getFileDescriptorResult(file: File): FileDescriptorResult { - var fileDescriptor: ParcelFileDescriptor? = null - var exception: Exception? = null - try { - file.createNewFile() - fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_TRUNCATE or ParcelFileDescriptor.MODE_READ_WRITE) - } catch (e: Exception) { - - exception = e - } - return FileDescriptorResult(fileDescriptor, exception) - } - - private class FileDescriptorResult( - private val fileDescriptor: ParcelFileDescriptor?, - private val exception: Exception? - ){ - fun isSuccess() = fileDescriptor != null - fun getSuccessData(): ParcelFileDescriptor = fileDescriptor!! - fun getFailureData(): Exception = exception!! - } - - private fun getPrintAdapter(webView: WebView): PrintDocumentAdapter = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) webView.createPrintDocumentAdapter("Document") - else webView.createPrintDocumentAdapter() - -} \ No newline at end of file diff --git a/pdfkit/src/main/java/com/mecofarid/pdfkit/ConversionHandler.kt b/pdfkit/src/main/java/com/mecofarid/pdfkit/ConversionHandler.kt deleted file mode 100644 index d4acca9..0000000 --- a/pdfkit/src/main/java/com/mecofarid/pdfkit/ConversionHandler.kt +++ /dev/null @@ -1,41 +0,0 @@ -package com.mecofarid.pdfkit - -import android.os.Handler -import android.os.HandlerThread -import android.os.Looper -import android.os.Message -import java.util.concurrent.LinkedBlockingQueue -import java.util.concurrent.ThreadPoolExecutor -import java.util.concurrent.TimeUnit - -private const val NAME = "ConversionHandler" -internal object ConversionHandler: HandlerThread(NAME){ - - /** - * As recommended in https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/AsyncTask.java - */ - private const val CORE_POOL_SIZE = 1 - private const val MAXIMUM_POOL_SIZE = 20 - private const val KEEP_ALIVE_SECONDS = 3L - private val handler by lazy { - object : Handler(looper) { - override fun handleMessage(msg: Message) { - threadPoolExecutor.execute(msg.obj as Runnable) - } - } - } - - private val threadPoolExecutor = ThreadPoolExecutor( - CORE_POOL_SIZE, - MAXIMUM_POOL_SIZE, - KEEP_ALIVE_SECONDS, - TimeUnit.SECONDS, - LinkedBlockingQueue() - ) - - internal fun execute(runnable: Runnable){ - if (!isAlive) - start() - handler.post(runnable) - } -} \ No newline at end of file diff --git a/pdfkit/src/main/java/com/mecofarid/pdfkit/PdfKit.kt b/pdfkit/src/main/java/com/mecofarid/pdfkit/PdfKit.kt index 865eeec..bcb71dd 100644 --- a/pdfkit/src/main/java/com/mecofarid/pdfkit/PdfKit.kt +++ b/pdfkit/src/main/java/com/mecofarid/pdfkit/PdfKit.kt @@ -2,19 +2,25 @@ package com.mecofarid.pdfkit import android.content.Context import android.os.Handler +import android.os.Looper import android.print.PrintAttributes import android.webkit.WebView import android.print.InternalPdfConverter +import android.provider.Contacts import android.util.Log import com.mecofarid.logger.Logger import java.io.File private const val PDF_EXTENSION = "pdf" -private const val TAG = "PdfKit" +private const val TAG = "PdfKit_TAG" class PdfKit(private val context: Context) { + private val mainThreadHandler by lazy { + Handler(Looper.getMainLooper()) + } fun startConversion( url: String, headers: Map = hashMapOf(), + javascriptEnabled: Boolean = false, printAttributes: PrintAttributes? = null, outputFile: File, onPdfPrintListener: OnPdfConversionListener @@ -28,6 +34,7 @@ class PdfKit(private val context: Context) { ) startConversion( printAttributes = printAttributes, + javascriptEnabled = javascriptEnabled, outputFile = outputFile, onPdfPrintListener = onPdfPrintListener, object : WebViewCreatedListener{ @@ -45,6 +52,7 @@ class PdfKit(private val context: Context) { encoding: String? = null, historyUrl: String? = null, printAttributes: PrintAttributes? = null, + javascriptEnabled: Boolean = false, outputFile: File, onPdfPrintListener: OnPdfConversionListener ) { @@ -61,6 +69,7 @@ class PdfKit(private val context: Context) { startConversion( printAttributes = printAttributes, + javascriptEnabled = javascriptEnabled, outputFile = outputFile, onPdfPrintListener = onPdfPrintListener, object : WebViewCreatedListener{ @@ -73,10 +82,12 @@ class PdfKit(private val context: Context) { private fun startConversion( printAttributes: PrintAttributes? = null, + javascriptEnabled: Boolean, outputFile: File, onPdfPrintListener: OnPdfConversionListener, onWebViewCreatedListener: WebViewCreatedListener ) { + Logger.d(TAG, "startConversion with: " + "PrintAttributes:$printAttributes " + "OutputFile:$outputFile" + @@ -89,13 +100,16 @@ class PdfKit(private val context: Context) { } - ConversionHandler.execute { + //Always post to main thread. Only WebView methods will be called on UI thread, conversion will be + // on non-UI thread check [InternalPdfConverter#startConversionInternal()] for details + mainThreadHandler.post { WebView(context).also { webView -> InternalPdfConverter.startConversion( - webView = webView, - printAttributes = printAttributes, - outputFile = outputFile, - onPdfPrintListener = onPdfPrintListener + webView = webView, + printAttributes = printAttributes, + javascriptEnabled = javascriptEnabled, + outputFile = outputFile, + onPdfPrintListener = onPdfPrintListener ) onWebViewCreatedListener.onWebViewCreated(webView) } @@ -113,5 +127,4 @@ class PdfKit(private val context: Context) { private fun File.isPdfFile() = this.extension.equals(other = PDF_EXTENSION, ignoreCase = true) - } \ No newline at end of file diff --git a/pdfkit/src/test/java/com/mecofarid/pdfkit/PdfKitTest.kt b/pdfkit/src/test/java/com/mecofarid/pdfkit/PdfKitTest.kt index 40917b3..0e5ca8f 100644 --- a/pdfkit/src/test/java/com/mecofarid/pdfkit/PdfKitTest.kt +++ b/pdfkit/src/test/java/com/mecofarid/pdfkit/PdfKitTest.kt @@ -2,6 +2,7 @@ package com.mecofarid.pdfkit import android.content.Context import android.os.Handler +import android.os.HandlerThread import android.os.Looper import android.os.Message import android.util.Log @@ -35,45 +36,39 @@ class PdfKitTest { @Test fun test_startConversion_1() { for (i in 0..100) { - Thread { - println("s ") - println("$i ${ConversionHandler.integ}") - }.start() + ConversionHandler.execute { + Log.d("TAG", "test_startConversion_1: ${Thread.currentThread().id}") + } } } - internal object ConversionHandler { + internal object ConversionHandler: HandlerThread("NAME"){ - /** - * As recommended in https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/AsyncTask.java - */ - private const val CORE_POOL_SIZE = 1 + private const val CORE_POOL_SIZE = 5 private const val MAXIMUM_POOL_SIZE = 20 private const val KEEP_ALIVE_SECONDS = 3L - val integ by lazy { - println("${Thread.currentThread()}") - Testo() - } - val handler by lazy { - Looper.prepare() - val handler = object : Handler(Looper.myLooper()!!) { + private val handler by lazy { + object : Handler(looper) { override fun handleMessage(msg: Message) { threadPoolExecutor.execute(msg.obj as Runnable) } } - Looper.loop() - handler } private val threadPoolExecutor = ThreadPoolExecutor( - CORE_POOL_SIZE, - MAXIMUM_POOL_SIZE, - KEEP_ALIVE_SECONDS, - TimeUnit.SECONDS, - LinkedBlockingQueue() - ) - } - + CORE_POOL_SIZE, + MAXIMUM_POOL_SIZE, + KEEP_ALIVE_SECONDS, + TimeUnit.SECONDS, + LinkedBlockingQueue() + ).also { + it.allowCoreThreadTimeOut(true) + } - class Testo + internal fun execute(runnable: Runnable){ + if (!isAlive) + start() + handler.post(runnable) + } + } } \ No newline at end of file