Commit 118a1bd6 authored by wanglei's avatar wanglei

...

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