|
@@ -0,0 +1,207 @@
|
|
|
+package com.compdfkit.conversion.flutter.kmpdfkit_demo
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import android.util.Log
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
+import com.compdfkit.conversion.*
|
|
|
+import com.compdfkit.conversion.flutter.kmpdfkit_demo.utils.FileUtils
|
|
|
+import io.flutter.embedding.engine.FlutterEngine
|
|
|
+import io.flutter.plugin.common.EventChannel
|
|
|
+import io.flutter.plugin.common.MethodCall
|
|
|
+import io.flutter.plugin.common.MethodChannel
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.GlobalScope
|
|
|
+import kotlinx.coroutines.flow.merge
|
|
|
+import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
+import java.io.File
|
|
|
+import java.lang.reflect.Method
|
|
|
+
|
|
|
+/**
|
|
|
+ * @classname:
|
|
|
+ * @author: LiuXiaoLong
|
|
|
+ * @date: 2023/2/23
|
|
|
+ * description:
|
|
|
+ */
|
|
|
+
|
|
|
+class ConvertFlutterPlugin(var activity: MainActivity, flutterPlugin: FlutterEngine) : EventChannel.StreamHandler {
|
|
|
+
|
|
|
+ val channelName = "com.compdfkit.conversion.flutter.convert"
|
|
|
+ val START_CONVERT = "start_convert"
|
|
|
+ val PARAMS_CONVERT_TYPE = "convertType"
|
|
|
+ val PARAMS_FILE_PATH = "filePath"
|
|
|
+ val PARAMS_FILE_NAME = "fileName"
|
|
|
+ val PARAMS_UPDATE_INDEX = "updateIndex"
|
|
|
+ val PARAMS_CONTAIN_IMAGES = "containImages"
|
|
|
+ val PARAMS_CONTAIN_ANNOTATIONS = "containAnnotations"
|
|
|
+ val PARAMS_WORK_SHEET_OPTIONS = "workSheetOptions"
|
|
|
+ val PARAMS_CONTENTOPTIONS = "contentOptions"
|
|
|
+ val PARAMS_IMAGE_DPI = "imageDpi"
|
|
|
+ val PARAMS_IMAGE_PAGE_OPTIONS = "imagePageOptions"
|
|
|
+ val PARAMS_MERGE_CSV = "mergeCSV"
|
|
|
+ val PARAMS_HTML_PAGE_OPTIONS = "htmlPageOptions"
|
|
|
+
|
|
|
+
|
|
|
+ val CONVERTING = 1
|
|
|
+ val CONVERT_SUCCESS = 2
|
|
|
+ val CONVERT_FAIL = 3
|
|
|
+
|
|
|
+ private var channel: EventChannel
|
|
|
+
|
|
|
+ init {
|
|
|
+ channel = EventChannel(flutterPlugin.dartExecutor.binaryMessenger, channelName)
|
|
|
+ channel.setStreamHandler(this)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
|
|
|
+ Log.e("转档Plugin", "arguments:${arguments}")
|
|
|
+ var convertInfoMap = arguments as Map<String, String>
|
|
|
+ val options = getConvertOptions(convertInfoMap[PARAMS_CONVERT_TYPE] ?: "", convertInfoMap)
|
|
|
+ val updateIndex = convertInfoMap[PARAMS_UPDATE_INDEX]
|
|
|
+ activity.lifecycleScope.launch(Dispatchers.IO) {
|
|
|
+ onConvert(context = activity,
|
|
|
+ filePath = convertInfoMap[PARAMS_FILE_PATH] ?: "",
|
|
|
+ convertType = convertInfoMap[PARAMS_CONVERT_TYPE] ?: "", pageArrays = null, params = options, onHandle = {
|
|
|
+
|
|
|
+ }, onProgress = { current, total ->
|
|
|
+ val progress = ((current / total.toFloat()) * 1000).toInt() / 10
|
|
|
+ val process = mapOf("progress" to progress, "updateIndex" to updateIndex, "status" to CONVERTING)
|
|
|
+ Log.e("转档Plugin", "进度:${progress}")
|
|
|
+ activity.lifecycleScope.launch(Dispatchers.Main){
|
|
|
+ events?.success(process)
|
|
|
+
|
|
|
+ }
|
|
|
+ }, onPost = { code, outFilePath ->
|
|
|
+ Log.e("转档Plugin", "onPost, code:${code.name}, outputPath:${outFilePath}")
|
|
|
+ when (code) {
|
|
|
+ ConvertError.ERR_SUCCESS -> {
|
|
|
+ val process = mapOf("progress" to 100, "updateIndex" to updateIndex, "status" to CONVERT_SUCCESS, "outputPath" to outFilePath)
|
|
|
+ activity.lifecycleScope.launch(Dispatchers.Main) {
|
|
|
+ events?.success(process)
|
|
|
+ events?.endOfStream()
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else -> {
|
|
|
+ val process = mapOf("progress" to 100, "updateIndex" to updateIndex, "status" to CONVERT_FAIL, "msg" to code.name)
|
|
|
+ activity.lifecycleScope.launch(Dispatchers.Main) {
|
|
|
+ events?.success(process)
|
|
|
+ events?.endOfStream()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCancel(arguments: Any?) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private fun getConvertOptions(convertType: String, optionsMap: Map<String, String>): CPDFConvertOptions {
|
|
|
+ val isContainImages = optionsMap[PARAMS_CONTAIN_IMAGES].toBoolean()
|
|
|
+ val isContainAnnotation = optionsMap[PARAMS_CONTAIN_ANNOTATIONS].toBoolean()
|
|
|
+ val workSheetOptions = WorkSheetOptions.valueOf(optionsMap[PARAMS_WORK_SHEET_OPTIONS]?:"")
|
|
|
+ val contentOptions = ContentOptions.valueOf(optionsMap[PARAMS_CONTENTOPTIONS]?:"")
|
|
|
+ val imageDpi = optionsMap[PARAMS_IMAGE_DPI]
|
|
|
+ val imagePageOptions = ImgType.valueOf(optionsMap[PARAMS_IMAGE_PAGE_OPTIONS]?:"PNG")
|
|
|
+ val mergeCSV = optionsMap[PARAMS_MERGE_CSV]?.toBoolean()?:false
|
|
|
+ val htmlPageOptions = PageAndNavigationPaneOptions.valueOf(optionsMap[PARAMS_HTML_PAGE_OPTIONS]?:"")
|
|
|
+ return when (ConvertType.valueOf(convertType.uppercase())) {
|
|
|
+ ConvertType.PPT -> CPDFConvertPPTOptions().also {
|
|
|
+ it.isContainImages = isContainImages
|
|
|
+ it.isContainAnnotations = isContainAnnotation
|
|
|
+ }
|
|
|
+ ConvertType.WORD -> CPDFConvertWordOptions().also {
|
|
|
+ it.isContainImages = isContainImages
|
|
|
+ it.isContainAnnotations = isContainAnnotation
|
|
|
+ }
|
|
|
+ ConvertType.EXCEL -> CPDFConvertExcelOptions().also {
|
|
|
+ it.isContainImages = isContainImages
|
|
|
+ it.isContainAnnotations = isContainAnnotation
|
|
|
+ it.workSheetOptions = workSheetOptions
|
|
|
+ it.contentOptions = contentOptions
|
|
|
+ }
|
|
|
+
|
|
|
+ ConvertType.TXT -> CPDFConvertTxtOptions()
|
|
|
+ ConvertType.IMAGE -> CPDFConvertImgOptions().also {
|
|
|
+ it.isContainAnnotations = isContainAnnotation
|
|
|
+ it.imageDpi = imageDpi?.toInt()?:300
|
|
|
+ it.imgType = imagePageOptions
|
|
|
+ }
|
|
|
+ ConvertType.CSV -> CPDFConvertCsvOptions().also {
|
|
|
+ it.isMergeCsv = mergeCSV
|
|
|
+ }
|
|
|
+ ConvertType.RTF -> CPDFConvertRtfOptions().also {
|
|
|
+ it.isContainImages = isContainImages
|
|
|
+ it.isContainAnnotations = isContainAnnotation
|
|
|
+ }
|
|
|
+ ConvertType.HTML -> CPDFConvertHtmlOptions().also {
|
|
|
+ it.isContainImages = isContainImages
|
|
|
+ it.isContainAnnotations = isContainAnnotation
|
|
|
+ it.pageAndNavigationPaneOptions = htmlPageOptions
|
|
|
+ }
|
|
|
+ else -> CPDFConvertOptions()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var cPDFConvert: CPDFConverter? = null
|
|
|
+
|
|
|
+ fun onConvert(
|
|
|
+ context: Context,
|
|
|
+ filePath: String,
|
|
|
+ convertType: String,
|
|
|
+ pageArrays: IntArray? = null,
|
|
|
+ params: CPDFConvertOptions,
|
|
|
+ onHandle: ((objptr: Long) -> Unit)? = null,
|
|
|
+ onProgress: ((current: Int, total: Int) -> Unit)? = null,
|
|
|
+ onPost: ((code: ConvertError, outFilePath: String?) -> Unit)? = null
|
|
|
+ ): ConvertError {
|
|
|
+ val outputDir = FileUtils.getOutputFilePath(context)
|
|
|
+ val uri = FileUtils.onGetUriBySystem(context, filePath) ?: return ConvertError.ERR_FILE
|
|
|
+ return when (ConvertType.valueOf(convertType.uppercase())) {
|
|
|
+ ConvertType.PPT -> CPDFConverterPPT(context, uri, "")
|
|
|
+ .run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ ConvertType.WORD -> CPDFConverterWord(context, uri, "")
|
|
|
+ .run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params as CPDFConvertWordOptions, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ ConvertType.EXCEL -> CPDFConverterExcel(context, uri, "")
|
|
|
+ .run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params as CPDFConvertExcelOptions, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ ConvertType.TXT -> CPDFConverterTxt(context, uri, "")
|
|
|
+ .run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params as CPDFConvertTxtOptions, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ ConvertType.IMAGE -> CPDFConverterImg(context, uri, "").run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ ConvertType.CSV -> CPDFConverterCsv(context, uri, "").run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ ConvertType.RTF -> CPDFConverterRtf(context, uri, "").run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ ConvertType.HTML -> CPDFConverterHtml(context, uri, "").run {
|
|
|
+ cPDFConvert = this
|
|
|
+ convert(outputDir, "", params, pageArrays, onHandle, onProgress, onPost)
|
|
|
+ }
|
|
|
+ else -> ConvertError.ERR_UNKNOWN
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|