Commit 118a1bd6 authored by wanglei's avatar wanglei

...

parent 2327df15
...@@ -12,7 +12,9 @@ import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentRenameDialo ...@@ -12,7 +12,9 @@ import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentRenameDialo
import com.base.pdfviewerscannerwhite.ui.view.PdfDialog.showPdfPwdDialog import com.base.pdfviewerscannerwhite.ui.view.PdfDialog.showPdfPwdDialog
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime2 import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime2
import com.base.pdfviewerscannerwhite.utils.LogEx import com.base.pdfviewerscannerwhite.utils.LogEx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.concurrent.ArrayBlockingQueue import java.util.concurrent.ArrayBlockingQueue
...@@ -47,62 +49,69 @@ class PdfMergeActivity : BaseActivity<ActivityPdfMergeBinding>() { ...@@ -47,62 +49,69 @@ class PdfMergeActivity : BaseActivity<ActivityPdfMergeBinding>() {
} }
binding.tvBtnNext.setOnClickListener { binding.tvBtnNext.setOnClickListener {
verificationPasswordDialogs { verificationPasswordDialogs {
showDocumentRenameDialog(name = "Merge_${System.currentTimeMillis().toFormatTime2()}", okAction = { name -> showDocumentRenameDialog(name = "Merge_${System.currentTimeMillis().toFormatTime2()}", okAction = { name ->
startActivity(Intent(this, PdfLoadingActivity::class.java).apply { startActivity(Intent(this, PdfLoadingActivity::class.java).apply {
putExtra("doWhat", DO_MERGE_PDF) putExtra("doWhat", DO_MERGE_PDF)
putExtra("newPath", pdfPresenter.createNewPdfPath(name)) putExtra("newPath", pdfPresenter.createNewPdfPath(name))
}) })
}) })
} }
} }
} }
private fun verificationPasswordDialogs(callBack: (isContinue: Boolean) -> Unit) { private fun verificationPasswordDialogs(callBack: () -> Unit) {
val queue: BlockingQueue<DocumentBean> = ArrayBlockingQueue(1) val queue: BlockingQueue<DocumentBean> = ArrayBlockingQueue(1)
val pwdItemList = mergePdfList.filter { it.state == 1 } val pwdItemList = mergePdfList.filter { it.state == 1 }.toMutableList()
if (pwdItemList.isNotEmpty()) { if (pwdItemList.isNotEmpty()) {
LogEx.logDebug(TAG, "verificationPasswordDialogs1") LogEx.logDebug(TAG, "verificationPasswordDialogs1")
val verifiedList = arrayListOf<DocumentBean>() val verifiedList = arrayListOf<DocumentBean>()
val first = pwdItemList[0] val first = pwdItemList[0]
queue.put(first) queue.put(first)
mergePdfList.removeAt(0) pwdItemList.removeAt(0)
var isCancel: Boolean = false
LogEx.logDebug(TAG, "verificationPasswordDialogs2") LogEx.logDebug(TAG, "verificationPasswordDialogs2")
lifecycleScope.launch() { lifecycleScope.launch(Dispatchers.IO) {
while (isActive) { while (isActive) {
val item: DocumentBean = queue.take() val item: DocumentBean = queue.take()
LogEx.logDebug(TAG, "verificationPasswordDialogs3") LogEx.logDebug(TAG, "pwdItem =${item.path}")
if (item.path == "Cancel") {
break
}
launch(Dispatchers.Main) {
showPdfPwdDialog(item.state, item.path, isCheckPwd = true, verificationAction = { pwd -> showPdfPwdDialog(item.state, item.path, isCheckPwd = true, verificationAction = { pwd ->
item.password = pwd item.password = pwd
verifiedList.add(item) verifiedList.add(item)
if (pwdItemList.isNotEmpty()) { if (pwdItemList.isNotEmpty()) {
val next = mergePdfList[0] val next = pwdItemList[0]
queue.put(next) queue.put(next)
pwdItemList.removeAt(0)
} else { } else {
cancel() queue.put(DocumentBean(path = "Cancel"))
} }
}, cancelAction = { }, cancelAction = {
isCancel = true
cancel() cancel()
}) })
} }
} }
if (isCancel) { LogEx.logDebug(TAG, "verificationPasswordDialogs3 ${verifiedList.size}")
callBack.invoke(false)
return
} else {
verifiedList.forEach { verifiedItem -> verifiedList.forEach { verifiedItem ->
mergePdfList.find { it.path == verifiedItem.path }?.password = verifiedItem.password mergePdfList.find { it.path == verifiedItem.path }?.password = verifiedItem.password
} }
}
mergePdfList.forEach { mergePdfList.forEach {
LogEx.logDebug(TAG, "密码=" + it.password) LogEx.logDebug(TAG, "密码=" + it.password)
} }
launch(Dispatchers.Main) {
callBack.invoke()
}
}
}else{
callBack.invoke()
} }
callBack.invoke(true)
} }
private fun initAdapter() { private fun initAdapter() {
......
...@@ -142,17 +142,27 @@ class PdfPresenter( ...@@ -142,17 +142,27 @@ class PdfPresenter(
val mergerUtility = PDFMergerUtility() val mergerUtility = PDFMergerUtility()
mergerUtility.destinationFileName = mergePath mergerUtility.destinationFileName = mergePath
LogEx.logDebug(TAG, "mergePdf ${mergePdfList.size}")
PdfMergeActivity.mergePdfList.forEach { documentBean -> PdfMergeActivity.mergePdfList.forEach { documentBean ->
LogEx.logDebug(TAG, "mergePdf item= ${documentBean.path} ${documentBean.state}")
if (documentBean.state == 0) { if (documentBean.state == 0) {
mergerUtility.addSource(File(documentBean.path)) mergerUtility.addSource(File(documentBean.path))
} else { } else {
LogEx.logDebug(TAG, "documentBean.password=${documentBean.password}") LogEx.logDebug(TAG, "mergePdf password=${documentBean.password}")
try {
val pdfDocument = PDDocument.load(File(documentBean.path), documentBean.password) val pdfDocument = PDDocument.load(File(documentBean.path), documentBean.password)
val byteArrayOutputStream = ByteArrayOutputStream() LogEx.logDebug(TAG, "mergePdf pdfDocument ${pdfDocument.numberOfPages}")
val byteArrayOutputStream = ByteArrayOutputStream(1024 * 2)
pdfDocument.save(byteArrayOutputStream) pdfDocument.save(byteArrayOutputStream)
LogEx.logDebug(TAG, "mergePdf byteArrayOutputStream ${byteArrayOutputStream.size()}")
pdfDocument.close() pdfDocument.close()
val inputStream = ByteArrayInputStream(byteArrayOutputStream.toByteArray()) val inputStream = ByteArrayInputStream(byteArrayOutputStream.toByteArray())
LogEx.logDebug(TAG, "mergePdf inputStream")
mergerUtility.addSource(inputStream) mergerUtility.addSource(inputStream)
} catch (e: Exception) {
LogEx.logDebug(TAG, "mergePdf Exception ${e.printStackTrace()}")
}
LogEx.logDebug(TAG, "mergePdf inputStream")
} }
} }
mergerUtility.mergeDocuments(null) mergerUtility.mergeDocuments(null)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment