|
@@ -5,9 +5,13 @@ import android.net.Uri
|
|
import android.util.Log
|
|
import android.util.Log
|
|
import androidx.lifecycle.LifecycleCoroutineScope
|
|
import androidx.lifecycle.LifecycleCoroutineScope
|
|
import com.compdfkit.core.document.CPDFDocument
|
|
import com.compdfkit.core.document.CPDFDocument
|
|
-import kotlinx.coroutines.Dispatchers
|
|
|
|
|
|
+import com.compdfkit.core.document.CPDFDocument.PDFDocumentError
|
|
|
|
+import com.pdf.base.impl.NormalOpenDocumentCallback
|
|
|
|
+import com.pdf.base.interfaces.OpenDocumentCallback
|
|
|
|
+import kotlinx.coroutines.*
|
|
|
|
+import kotlinx.coroutines.flow.*
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.launch
|
|
-import kotlinx.coroutines.withContext
|
|
|
|
|
|
+import kotlin.coroutines.suspendCoroutine
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author: hubowen
|
|
* @author: hubowen
|
|
@@ -20,95 +24,75 @@ import kotlinx.coroutines.withContext
|
|
* onSuccess 返回一个正常的Document
|
|
* onSuccess 返回一个正常的Document
|
|
* onError 错误返回
|
|
* onError 错误返回
|
|
*/
|
|
*/
|
|
-fun LifecycleCoroutineScope.onVerifyDocument(context: Context, uri: Uri?, absolutePath: String?, password: String? = "", onSuccess: ((CPDFDocument) -> Unit)?, onError: (() -> Unit)? = null) {
|
|
|
|
- launch(Dispatchers.IO) {
|
|
|
|
- context.toCPDFDocument(uri, absolutePath, password, {
|
|
|
|
- onSuccess?.invoke(it)
|
|
|
|
- }, {
|
|
|
|
- onError?.invoke()
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+private suspend fun CPDFDocument.openDocument(uri: Uri?, absolutePath: String?, password: String? = ""): Flow<Pair<PDFDocumentError, CPDFDocument?>> {
|
|
|
|
+ return flow {
|
|
|
|
+ val result = try {
|
|
|
|
+ val error =
|
|
|
|
+ when {
|
|
|
|
+ uri != null -> if (password.isNullOrEmpty()) open(uri) else open(uri, password)
|
|
|
|
+ absolutePath.isNullOrEmpty().not() -> if (password.isNullOrEmpty()) open(absolutePath) else open(absolutePath, password)
|
|
|
|
+ else -> {
|
|
|
|
+ PDFDocumentError.PDFDocumentErrorUnknown
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ error to this@openDocument
|
|
|
|
+ } catch (e: Exception) {
|
|
|
|
+ e.printStackTrace()
|
|
|
|
+ PDFDocumentError.PDFDocumentErrorUnknown to null
|
|
|
|
+ }
|
|
|
|
+ emit(result)
|
|
|
|
+ }.flowOn(Dispatchers.IO)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 转换至 Document对象,这里单独写出来,提供单独的document,用来做功能
|
|
* 转换至 Document对象,这里单独写出来,提供单独的document,用来做功能
|
|
* onSuccess 返回一个正常的Document
|
|
* onSuccess 返回一个正常的Document
|
|
* NeedExit 错误返回
|
|
* NeedExit 错误返回
|
|
*/
|
|
*/
|
|
-suspend fun Context.toCPDFDocument(uri: Uri?, absolutePath: String?, password: String? = "", onSuccess: ((CPDFDocument) -> Unit)?, onError: (() -> Unit)? = null) {
|
|
|
|
- var document: CPDFDocument? = null
|
|
|
|
-
|
|
|
|
- fun getCPDFDocument(password: String? = null): CPDFDocument.PDFDocumentError? {
|
|
|
|
- return try {
|
|
|
|
- when (absolutePath.isNullOrEmpty()) {
|
|
|
|
- true -> {
|
|
|
|
- uri?.let { uri_ ->
|
|
|
|
- CPDFDocument(this).let {
|
|
|
|
- document = it
|
|
|
|
- when {
|
|
|
|
- password.isNullOrEmpty() -> it.open(uri_)
|
|
|
|
- else -> it.open(uri_, password)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else -> {
|
|
|
|
- CPDFDocument(this).let {
|
|
|
|
- document = it
|
|
|
|
- when {
|
|
|
|
- password.isNullOrEmpty() -> it.open(absolutePath)
|
|
|
|
- else -> it.open(absolutePath, password)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+suspend fun Context.openDocument(uri: Uri?, absolutePath: String?, password: String? = "", callback: NormalOpenDocumentCallback.() -> Unit) {
|
|
|
|
+ val listener = NormalOpenDocumentCallback().also(callback)
|
|
|
|
+ val document = CPDFDocument(this)
|
|
|
|
+ document.openDocument(uri, absolutePath, password).collect {
|
|
|
|
+ when (it.first) {
|
|
|
|
+ PDFDocumentError.PDFDocumentErrorSuccess -> listener.success(cPdfDocument = it.second!!)
|
|
|
|
+ PDFDocumentError.PDFDocumentErrorPassword -> {
|
|
|
|
+ listener.isEncrypt(it.second!!)
|
|
}
|
|
}
|
|
- } catch (e: Exception) {
|
|
|
|
- e.printStackTrace()
|
|
|
|
- onError?.invoke()
|
|
|
|
- null
|
|
|
|
|
|
+ else -> listener.fail(error = it.first)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
|
|
- runCatching {
|
|
|
|
- getCPDFDocument(password)?.apply {
|
|
|
|
- withContext(Dispatchers.Main) {
|
|
|
|
- when (this@apply) {
|
|
|
|
- CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess -> {
|
|
|
|
- document?.run { onSuccess?.invoke(this) }
|
|
|
|
- }
|
|
|
|
- CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword -> {
|
|
|
|
- Log.e("AAAA", "PDFDocumentErrorPassword")
|
|
|
|
-// (ActivitysUtils.current() as AppCompatActivity?)?.apply {
|
|
|
|
-// PdfPasswordEnterDialog(tPdfDocument, uri, absolutePath).onShow(supportFragmentManager) {
|
|
|
|
-// when {
|
|
|
|
-// it -> onFinish?.invoke(tPdfDocument)
|
|
|
|
-// else -> needExit?.invoke()
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
- }
|
|
|
|
- else -> {
|
|
|
|
- Log.e("AAAA", this@apply.toString())
|
|
|
|
-// (ActivitysUtils.current() as AppCompatActivity?)?.let {
|
|
|
|
-// BaseDialogFragmentHelper.showConfirmTips(
|
|
|
|
-// it.supportFragmentManager,
|
|
|
|
-// title = getString(R.string.pdf_damaged_title),
|
|
|
|
-// message = getString(R.string.pdf_damaged_content),
|
|
|
|
-// isCancelable = false,
|
|
|
|
-// isCanceledOnTouchOutside = false
|
|
|
|
-// ) {
|
|
|
|
-// needExit?.invoke()
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 重试打开文档,不会再调用回调中的isEncrypt()
|
|
|
|
+ */
|
|
|
|
+suspend fun CPDFDocument.retryOpenDocument(uri: Uri?, absolutePath: String?, password: String? = "", normalOpenDocumentCallback: NormalOpenDocumentCallback) {
|
|
|
|
+ openDocument(uri, absolutePath, password).collect{ pair->
|
|
|
|
+ when (pair.first) {
|
|
|
|
+ PDFDocumentError.PDFDocumentErrorSuccess -> normalOpenDocumentCallback.success(cPdfDocument = pair.second!!)
|
|
|
|
+ else -> normalOpenDocumentCallback.fail(error = pair.first)
|
|
}
|
|
}
|
|
- }.onFailure {
|
|
|
|
- onError?.invoke()
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * 文档是否已加密
|
|
|
|
+ * @param uri 要检查的Uri文件
|
|
|
|
+ * @param absolutePath 要检查的file 路径文件
|
|
|
|
+ */
|
|
|
|
+suspend fun Context.documentIsEncrypt(uri: Uri? = null, absolutePath: String? = ""): Boolean {
|
|
|
|
+ val document = CPDFDocument(this)
|
|
|
|
+ val result = document.openDocument(uri = uri, absolutePath = absolutePath, password = "")
|
|
|
|
+ .firstOrNull()
|
|
|
|
+ return result != null && result.first == PDFDocumentError.PDFDocumentErrorPassword
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 保存Document 这里有一个逻辑,先增量保存如果失败则进行全量保存。
|
|
* 保存Document 这里有一个逻辑,先增量保存如果失败则进行全量保存。
|
|
*/
|
|
*/
|