Mercurial > dwindows
comparison android/DWindows.kt @ 2727:bf585f375286
Android: Initial print implementation for Android....
Currently crashes shortly after displaying printer selection dialog.
Fixes coming but I wanted to get the bulk of it committed.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 11 Dec 2021 14:14:26 +0000 |
parents | cacde852e2db |
children | 34e34d5d56e8 |
comparison
equal
deleted
inserted
replaced
2726:7a15401e73f4 | 2727:bf585f375286 |
---|---|
65 import android.util.Base64 | 65 import android.util.Base64 |
66 import kotlin.math.* | 66 import kotlin.math.* |
67 import android.content.ContentUris | 67 import android.content.ContentUris |
68 import androidx.appcompat.widget.AppCompatSeekBar | 68 import androidx.appcompat.widget.AppCompatSeekBar |
69 import android.content.DialogInterface | 69 import android.content.DialogInterface |
70 | 70 import android.graphics.pdf.PdfDocument |
71 | 71 import android.print.* |
72 | 72 import android.print.pdf.PrintedPdfDocument |
73 | 73 |
74 | 74 |
75 // Color Wheel section | 75 // Color Wheel section |
76 private val HUE_COLORS = intArrayOf( | 76 private val HUE_COLORS = intArrayOf( |
77 Color.RED, | 77 Color.RED, |
944 } | 944 } |
945 | 945 |
946 external fun eventHandlerHTMLChanged(obj1: View, message: Int, URI: String, status: Int) | 946 external fun eventHandlerHTMLChanged(obj1: View, message: Int, URI: String, status: Int) |
947 } | 947 } |
948 | 948 |
949 class DWPrintDocumentAdapter : PrintDocumentAdapter() | |
950 { | |
951 var context: Context? = null | |
952 var pages: Int = 0 | |
953 var pdfDocument: PrintedPdfDocument? = null | |
954 var print: Long = 0 | |
955 | |
956 override fun onLayout( | |
957 oldAttributes: PrintAttributes?, | |
958 newAttributes: PrintAttributes, | |
959 cancellationSignal: CancellationSignal?, | |
960 callback: LayoutResultCallback, | |
961 extras: Bundle? | |
962 ) { | |
963 // Create a new PdfDocument with the requested page attributes | |
964 pdfDocument = context?.let { PrintedPdfDocument(it, newAttributes) } | |
965 | |
966 // Respond to cancellation request | |
967 if (cancellationSignal?.isCanceled == true) { | |
968 callback.onLayoutCancelled() | |
969 return | |
970 } | |
971 | |
972 if (pages > 0) { | |
973 // Return print information to print framework | |
974 PrintDocumentInfo.Builder("print_output.pdf") | |
975 .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT) | |
976 .setPageCount(pages) | |
977 .build() | |
978 .also { info -> | |
979 // Content layout reflow is complete | |
980 callback.onLayoutFinished(info, true) | |
981 } | |
982 } else { | |
983 // Otherwise report an error to the print framework | |
984 callback.onLayoutFailed("No pages to print.") | |
985 } | |
986 } | |
987 | |
988 override fun onWrite( | |
989 pageRanges: Array<out PageRange>, | |
990 destination: ParcelFileDescriptor, | |
991 cancellationSignal: CancellationSignal?, | |
992 callback: WriteResultCallback | |
993 ) { | |
994 var writtenPagesArray: Array<PdfDocument.Page> = emptyArray() | |
995 | |
996 // Iterate over each page of the document, | |
997 // check if it's in the output range. | |
998 for (i in 0 until pages) { | |
999 pdfDocument?.startPage(i)?.also { page -> | |
1000 | |
1001 // check for cancellation | |
1002 if (cancellationSignal?.isCanceled == true) { | |
1003 callback.onWriteCancelled() | |
1004 pdfDocument?.close() | |
1005 pdfDocument = null | |
1006 return | |
1007 } | |
1008 | |
1009 // Draw page content for printing | |
1010 var bitmap = Bitmap.createBitmap(page.canvas.width, page.canvas.height, Bitmap.Config.ARGB_8888) | |
1011 // Actual drawing is done in the JNI C code callback to the bitmap | |
1012 eventHandlerPrintDraw(print, bitmap, i, page.canvas.width, page.canvas.height) | |
1013 // Copy from the bitmap canvas our C code drew on to the PDF page canvas | |
1014 val rect = Rect(0, 0, page.canvas.width, page.canvas.height) | |
1015 page.canvas.drawBitmap(bitmap, rect, rect, null) | |
1016 | |
1017 // Rendering is complete, so page can be finalized. | |
1018 pdfDocument?.finishPage(page) | |
1019 | |
1020 // Add the new page to the array | |
1021 writtenPagesArray += page | |
1022 } | |
1023 } | |
1024 | |
1025 // Write PDF document to file | |
1026 try { | |
1027 pdfDocument?.writeTo(FileOutputStream(destination.fileDescriptor)) | |
1028 } catch (e: IOException) { | |
1029 callback.onWriteFailed(e.toString()) | |
1030 return | |
1031 } finally { | |
1032 pdfDocument?.close() | |
1033 pdfDocument = null | |
1034 } | |
1035 // Signal the print framework the document is complete | |
1036 callback.onWriteFinished(pageRanges) | |
1037 } | |
1038 | |
1039 override fun onFinish() { | |
1040 // Notify our C code so it can cleanup | |
1041 eventHandlerPrintFinish(print) | |
1042 super.onFinish() | |
1043 } | |
1044 | |
1045 external fun eventHandlerPrintDraw(print: Long, bitmap: Bitmap, page: Int, width: Int, height: Int) | |
1046 external fun eventHandlerPrintFinish(print: Long) | |
1047 } | |
1048 | |
949 class DWSlider | 1049 class DWSlider |
950 @JvmOverloads constructor(context: Context): FrameLayout(context) { | 1050 @JvmOverloads constructor(context: Context): FrameLayout(context) { |
951 val slider: SeekBar = SeekBar(context) | 1051 val slider: SeekBar = SeekBar(context) |
952 | 1052 |
953 init { | 1053 init { |
4654 } | 4754 } |
4655 } | 4755 } |
4656 } | 4756 } |
4657 } | 4757 } |
4658 return pixmap | 4758 return pixmap |
4759 } | |
4760 | |
4761 fun printRun(print: Long, flags: Int, jobname: String, pages: Int, runflags: Int): PrintJob? | |
4762 { | |
4763 var retval: PrintJob? = null | |
4764 | |
4765 waitOnUiThread { | |
4766 // Get a PrintManager instance | |
4767 val printManager = this.getSystemService(Context.PRINT_SERVICE) as PrintManager | |
4768 // Setup our print adapter | |
4769 val printAdapter = DWPrintDocumentAdapter() | |
4770 printAdapter.context = this | |
4771 printAdapter.pages = pages | |
4772 printAdapter.print = print | |
4773 // Start a print job, passing in a PrintDocumentAdapter implementation | |
4774 // to handle the generation of a print document | |
4775 retval = printManager.print(jobname, printAdapter, null) | |
4776 } | |
4777 return retval | |
4778 } | |
4779 | |
4780 fun printCancel(printjob: PrintJob) | |
4781 { | |
4782 waitOnUiThread { | |
4783 // Get a PrintManager instance | |
4784 val printManager = this.getSystemService(Context.PRINT_SERVICE) as PrintManager | |
4785 // Remove the job we earlier added from the queue | |
4786 printManager.printJobs.remove(printjob) | |
4787 } | |
4659 } | 4788 } |
4660 | 4789 |
4661 fun pixmapGetDimensions(pixmap: Bitmap): Long | 4790 fun pixmapGetDimensions(pixmap: Bitmap): Long |
4662 { | 4791 { |
4663 var dimensions: Long = 0 | 4792 var dimensions: Long = 0 |