allow printing PDFs if we know the path

This commit is contained in:
tibbi 2022-05-15 21:24:40 +02:00
parent c687e1bd27
commit 712aa64bb1
3 changed files with 114 additions and 0 deletions

View File

@ -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())
}
}
}

View File

@ -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<PageRange>,
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) {
}
}
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="AppCompatResource">
<item
android:id="@+id/menu_print"
android:showAsAction="never"
android:title="@string/print"
app:showAsAction="never" />
</menu>