desc:cmd copy

main
xiaowusky 2 years ago
parent 336cb75476
commit 0e75caf0e1

@ -19,6 +19,10 @@ import com.yinuo.safetywatcher.xls.SimpleCellValue
import com.yinuo.safetywatcher.xls.utils.ExcelUtils import com.yinuo.safetywatcher.xls.utils.ExcelUtils
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.IOException
import java.io.InputStreamReader
object TestUtils { object TestUtils {
@ -111,8 +115,71 @@ object TestUtils {
row.add(SimpleCellValue(it.unit)) row.add(SimpleCellValue(it.unit))
allData.add(row) 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<String>,
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()
}
} }
} }

@ -95,7 +95,7 @@ object ExcelUtils {
fun writeStringListToExcel( fun writeStringListToExcel(
allRowsData: List<List<ICellValue>>, allRowsData: List<List<ICellValue>>,
context: Context context: Context
): Boolean { ): String? {
val fileName = PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN) + ".xls" val fileName = PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN) + ".xls"
PathUtils.EXCEL_EXPORT_NAME = fileName PathUtils.EXCEL_EXPORT_NAME = fileName
val filePath = val filePath =
@ -107,7 +107,7 @@ object ExcelUtils {
initExcel(columns, filePath, PathUtils.SHEET_NAME) //需要写入权限 initExcel(columns, filePath, PathUtils.SHEET_NAME) //需要写入权限
if (PathUtils.isListEmpty(allRowsData) || context == null) if (PathUtils.isListEmpty(allRowsData) || context == null)
return false return null
var writebook: WritableWorkbook? = null var writebook: WritableWorkbook? = null
var inputStream: InputStream? = null var inputStream: InputStream? = null
@ -126,13 +126,13 @@ object ExcelUtils {
} }
writebook.write() writebook.write()
Log.i(TAG, "Excelel 写入成功") Log.i(TAG, "Excelel 写入成功")
return true return filePath
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "writeStringListToExcel() e==" + e.message) Log.e(TAG, "writeStringListToExcel() e==" + e.message)
} finally { } finally {
writebook?.close() writebook?.close()
inputStream?.close() inputStream?.close()
} }
return false return null
} }
} }

@ -2,6 +2,8 @@ package com.yinuo.safetywatcher.xls.utils
import android.content.Context import android.content.Context
import android.os.Environment import android.os.Environment
import android.os.StatFs
import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
@ -24,6 +26,7 @@ object PathUtils {
* 获取应用中文件存储 * 获取应用中文件存储
*/ */
fun getExternalStoragePath(context: Context): String? { fun getExternalStoragePath(context: Context): String? {
return LztekUtil.getLztek()?.usbStoragePath
return context.getExternalFilesDir(null)?.path return context.getExternalFilesDir(null)?.path
} }

Loading…
Cancel
Save