From 712aa64bb1590c39b8b4af77192d7050a2522a88 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 15 May 2022 21:24:40 +0200 Subject: [PATCH] allow printing PDFs if we know the path --- .../pro/activities/PDFViewerActivity.kt | 40 ++++++++++++ .../pro/helpers/PdfDocumentAdapter.kt | 63 +++++++++++++++++++ app/src/main/res/menu/menu_pdf_viewer.xml | 11 ++++ 3 files changed, 114 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/PdfDocumentAdapter.kt create mode 100644 app/src/main/res/menu/menu_pdf_viewer.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/PDFViewerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/PDFViewerActivity.kt index 9c0ee515..92c865a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/PDFViewerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/PDFViewerActivity.kt @@ -1,13 +1,23 @@ package com.simplemobiletools.filemanager.pro.activities +import android.content.Context import android.os.Bundle +import android.print.PrintAttributes +import android.print.PrintManager +import android.view.Menu +import android.view.MenuItem import com.simplemobiletools.commons.extensions.checkAppSideloading +import com.simplemobiletools.commons.extensions.getFilenameFromPath import com.simplemobiletools.commons.extensions.getFilenameFromUri import com.simplemobiletools.commons.extensions.getProperBackgroundColor +import com.simplemobiletools.commons.helpers.REAL_FILE_PATH import com.simplemobiletools.filemanager.pro.R +import com.simplemobiletools.filemanager.pro.helpers.PdfDocumentAdapter import kotlinx.android.synthetic.main.activity_pdf_viewer.* class PDFViewerActivity : SimpleActivity() { + var realFilePath = "" + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_pdf_viewer) @@ -16,9 +26,31 @@ class PDFViewerActivity : SimpleActivity() { return } + if (intent.extras?.containsKey(REAL_FILE_PATH) == true) { + realFilePath = intent.extras?.get(REAL_FILE_PATH)?.toString() ?: "" + } + checkIntent() } + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.menu_pdf_viewer, menu) + menu.apply { + findItem(R.id.menu_print).isVisible = realFilePath.isNotEmpty() + } + + updateMenuItemColors(menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.menu_print -> printText() + else -> return super.onOptionsItemSelected(item) + } + return true + } + private fun checkIntent() { val uri = intent.data if (uri == null) { @@ -36,4 +68,12 @@ class PDFViewerActivity : SimpleActivity() { .spacing(15) .load() } + + private fun printText() { + val adapter = PdfDocumentAdapter(this, realFilePath) + + (getSystemService(Context.PRINT_SERVICE) as? PrintManager)?.apply { + print(realFilePath.getFilenameFromPath(), adapter, PrintAttributes.Builder().build()) + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/PdfDocumentAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/PdfDocumentAdapter.kt new file mode 100644 index 00000000..83202a61 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/helpers/PdfDocumentAdapter.kt @@ -0,0 +1,63 @@ +package com.simplemobiletools.filemanager.pro.helpers + +import android.content.Context +import android.os.Bundle +import android.os.CancellationSignal +import android.os.ParcelFileDescriptor +import android.print.PageRange +import android.print.PrintAttributes +import android.print.PrintDocumentAdapter +import android.print.PrintDocumentInfo +import com.simplemobiletools.commons.extensions.getFilenameFromPath +import java.io.* + +// taken from https://github.com/HarshitaBambure/AndroidPDFPrint/blob/master/app/src/main/java/com/example/androidpdfprint/PdfDocumentAdapter.java +class PdfDocumentAdapter(var context: Context, var path: String) : PrintDocumentAdapter() { + override fun onLayout( + oldAttributes: PrintAttributes, + printAttributes: PrintAttributes, + cancellationSignal: CancellationSignal, + layoutResultCallback: LayoutResultCallback, + extras: Bundle + ) { + if (cancellationSignal.isCanceled) layoutResultCallback.onLayoutCancelled() else { + val builder = PrintDocumentInfo.Builder(path.getFilenameFromPath()) + builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT) + .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN) + .build() + layoutResultCallback.onLayoutFinished(builder.build(), printAttributes != printAttributes) + } + } + + override fun onWrite( + pages: Array, + parcelFileDescriptor: ParcelFileDescriptor, + cancellationSignal: CancellationSignal, + writeResultCallback: WriteResultCallback + ) { + var inputStream: InputStream? = null + var outputStream: OutputStream? = null + try { + val file = File(path) + inputStream = FileInputStream(file) + outputStream = FileOutputStream(parcelFileDescriptor.fileDescriptor) + val buff = ByteArray(16384) + var size: Int + while (inputStream.read(buff).also { size = it } >= 0 && !cancellationSignal.isCanceled) { + outputStream.write(buff, 0, size) + } + + if (cancellationSignal.isCanceled) writeResultCallback.onWriteCancelled() else { + writeResultCallback.onWriteFinished(arrayOf(PageRange.ALL_PAGES)) + } + } catch (e: Exception) { + writeResultCallback.onWriteFailed(e.message) + } finally { + try { + inputStream!!.close() + outputStream!!.close() + } catch (ex: IOException) { + } + } + } +} diff --git a/app/src/main/res/menu/menu_pdf_viewer.xml b/app/src/main/res/menu/menu_pdf_viewer.xml new file mode 100644 index 00000000..05093c7b --- /dev/null +++ b/app/src/main/res/menu/menu_pdf_viewer.xml @@ -0,0 +1,11 @@ + + + +