diff --git a/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt b/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt index 4141991..95843c2 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt @@ -19,6 +19,10 @@ import com.yinuo.safetywatcher.xls.SimpleCellValue import com.yinuo.safetywatcher.xls.utils.ExcelUtils import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch +import java.io.BufferedReader +import java.io.DataOutputStream +import java.io.IOException +import java.io.InputStreamReader object TestUtils { @@ -111,8 +115,71 @@ object TestUtils { row.add(SimpleCellValue(it.unit)) allData.add(row) } - ExcelUtils.writeStringListToExcel(allData, context); + val excelPath = ExcelUtils.writeStringListToExcel(allData, context) +// val dataPath = LztekUtil.getLztek()?.usbStoragePath + "/" +// // 直接copy到机身 +// val command = arrayOf("remount","cp -r $excelPath $dataPath") +// execCommand(command, true, true) + commonDialog?.dismiss() + } - commonDialog?.dialogBinding?.root?.postDelayed({ commonDialog.dismiss() }, 1000) + // ShellUtils execCommand()方法 + fun execCommand( + commands: Array, + isRoot: Boolean, + isNeedResultMsg: Boolean + ) { + var result = -1 + if (commands.isEmpty()) { + return + } + var process: Process? = null + var successResult: BufferedReader? = null + var errorResult: BufferedReader? = null + var successMsg: StringBuilder? = null + var errorMsg: StringBuilder? = null + var os: DataOutputStream? = null + try { + process = Runtime.getRuntime().exec(if (isRoot) "su" else "sh") + os = DataOutputStream(process!!.outputStream) + for (command in commands) { + if (command == null) { + continue + } + + // do not use os.writeBytes(command), avoid chinese charset error + os.write(command.toByteArray()) + os.writeBytes("\n") + os.flush() + } + os.writeBytes("exit\n") + os.flush() + result = process.waitFor() + // get command result + if (isNeedResultMsg) { + successMsg = StringBuilder() + errorMsg = StringBuilder() + successResult = BufferedReader(InputStreamReader(process.inputStream)) + errorResult = BufferedReader(InputStreamReader(process.errorStream)) + var s: String? + while (successResult.readLine().also { s = it } != null) { + successMsg.append(s) + } + while (errorResult.readLine().also { s = it } != null) { + errorMsg.append(s) + } + } + } catch (e: Exception) { + e.printStackTrace() + } finally { + try { + os?.close() + successResult?.close() + errorResult?.close() + } catch (e: IOException) { + e.printStackTrace() + } + process?.destroy() + } } } \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/xls/utils/ExcelUtils.kt b/app/src/main/java/com/yinuo/safetywatcher/xls/utils/ExcelUtils.kt index 607e728..fa58161 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/xls/utils/ExcelUtils.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/xls/utils/ExcelUtils.kt @@ -95,7 +95,7 @@ object ExcelUtils { fun writeStringListToExcel( allRowsData: List>, context: Context - ): Boolean { + ): String? { val fileName = PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN) + ".xls" PathUtils.EXCEL_EXPORT_NAME = fileName val filePath = @@ -107,7 +107,7 @@ object ExcelUtils { initExcel(columns, filePath, PathUtils.SHEET_NAME) //需要写入权限 if (PathUtils.isListEmpty(allRowsData) || context == null) - return false + return null var writebook: WritableWorkbook? = null var inputStream: InputStream? = null @@ -126,13 +126,13 @@ object ExcelUtils { } writebook.write() Log.i(TAG, "Excelel 写入成功") - return true + return filePath } catch (e: Exception) { Log.e(TAG, "writeStringListToExcel() e==" + e.message) } finally { writebook?.close() inputStream?.close() } - return false + return null } } \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/xls/utils/PathUtils.kt b/app/src/main/java/com/yinuo/safetywatcher/xls/utils/PathUtils.kt index cdc3dfe..f86bd85 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/xls/utils/PathUtils.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/xls/utils/PathUtils.kt @@ -2,6 +2,8 @@ package com.yinuo.safetywatcher.xls.utils import android.content.Context import android.os.Environment +import android.os.StatFs +import com.yinuo.safetywatcher.watcher.utils.LztekUtil import java.text.SimpleDateFormat import java.util.Date import java.util.Locale @@ -24,6 +26,7 @@ object PathUtils { * 获取应用中文件存储 */ fun getExternalStoragePath(context: Context): String? { + return LztekUtil.getLztek()?.usbStoragePath return context.getExternalFilesDir(null)?.path }