desc:导出修改
parent
1a94d22519
commit
aeee3662bf
@ -1,202 +0,0 @@
|
|||||||
package com.yinuo.safetywatcher
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.media.MediaCodecList
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Build
|
|
||||||
import android.os.FileUtils
|
|
||||||
import android.provider.Settings
|
|
||||||
import android.util.Log
|
|
||||||
import com.common.commonlib.db.DBUtils
|
|
||||||
import com.common.commonlib.db.entity.Gas
|
|
||||||
import com.common.commonlib.db.entity.Video
|
|
||||||
import com.common.commonlib.db.entity.Warning
|
|
||||||
import com.yinuo.safetywatcher.watcher.net.UploadFileApi
|
|
||||||
import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog
|
|
||||||
import com.yinuo.safetywatcher.watcher.utils.DateUtils
|
|
||||||
import com.yinuo.safetywatcher.watcher.xls.SimpleCellValue
|
|
||||||
import com.yinuo.safetywatcher.watcher.xls.utils.ExcelUtils
|
|
||||||
import kotlinx.coroutines.GlobalScope
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import org.easydarwin.PushHelper
|
|
||||||
import java.io.BufferedReader
|
|
||||||
import java.io.DataOutputStream
|
|
||||||
import java.io.File
|
|
||||||
import java.io.IOException
|
|
||||||
import java.io.InputStreamReader
|
|
||||||
|
|
||||||
|
|
||||||
object TestUtils {
|
|
||||||
|
|
||||||
|
|
||||||
fun insertData() {
|
|
||||||
getSupportCodec()
|
|
||||||
GlobalScope.launch() {
|
|
||||||
// 构造气体数据
|
|
||||||
val timeMillis = System.currentTimeMillis()
|
|
||||||
|
|
||||||
// 构造告警数据
|
|
||||||
val warningDao = DBUtils.warningDao()
|
|
||||||
val warnings = mutableListOf<Warning>()
|
|
||||||
warnings.add(Warning("CO", 60f, "", timeMillis))
|
|
||||||
warnings.add(Warning("CO", 80f, "", timeMillis - 30 * 1000))
|
|
||||||
warnings.add(Warning("CO2", 80f, "", timeMillis - 30 * 1000))
|
|
||||||
warnings.add(Warning("O2", 10f, "", timeMillis - 90 * 1000))
|
|
||||||
warningDao.insertAll(warnings)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun requestReadNetworkStats(context: Context) {
|
|
||||||
val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
// 经过测试,只有在 Android 10 及以上加包名才有效果
|
|
||||||
// 如果在 Android 10 以下加包名会导致无法跳转
|
|
||||||
intent.data = Uri.parse("package:" + context.packageName)
|
|
||||||
}
|
|
||||||
context.startActivity(intent)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun testUploadFile(uploadApi: UploadFileApi, video: Video, commonDialog: CommonDialog?) {
|
|
||||||
// val path = video.path
|
|
||||||
// uploadApi.singleUpload(path, System.currentTimeMillis(), object :
|
|
||||||
// RequestResultCallBack<BaseResponse>() {
|
|
||||||
// override fun onResult(result: BaseResponse) {
|
|
||||||
// commonDialog?.dismiss()
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// override fun onError(error: String?) {
|
|
||||||
// commonDialog?.dismiss()
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun testExportExcel(
|
|
||||||
context: Context,
|
|
||||||
usbPath: String,
|
|
||||||
datas: MutableList<Gas>,
|
|
||||||
commonDialog: CommonDialog?
|
|
||||||
) {
|
|
||||||
val allData = mutableListOf<List<SimpleCellValue>>()
|
|
||||||
datas.forEach {
|
|
||||||
val row = mutableListOf<SimpleCellValue>()
|
|
||||||
row.add(SimpleCellValue(DateUtils.formatHistoryTime(it.time) ?: ""))
|
|
||||||
row.add(SimpleCellValue(it.gasName))
|
|
||||||
row.add(SimpleCellValue(it.gasValue.toString()))
|
|
||||||
row.add(SimpleCellValue(it.unit))
|
|
||||||
allData.add(row)
|
|
||||||
}
|
|
||||||
val excelPath = ExcelUtils.writeStringListToExcel(allData, usbPath, context)
|
|
||||||
commonDialog?.dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
suspend fun testExportWarnExcel(
|
|
||||||
context: Context,
|
|
||||||
usbPath: String,
|
|
||||||
datas: MutableList<Warning>,
|
|
||||||
loadingDialog: CommonDialog?
|
|
||||||
) {
|
|
||||||
val allData = mutableListOf<List<SimpleCellValue>>()
|
|
||||||
datas.forEach {
|
|
||||||
val row = mutableListOf<SimpleCellValue>()
|
|
||||||
row.add(SimpleCellValue(DateUtils.formatHistoryTime(it.startTime) ?: ""))
|
|
||||||
row.add(SimpleCellValue(it.gasName))
|
|
||||||
row.add(SimpleCellValue(it.gasValue.toString()))
|
|
||||||
row.add(SimpleCellValue(it.unit))
|
|
||||||
allData.add(row)
|
|
||||||
}
|
|
||||||
val excelPath = ExcelUtils.writeStringListToExcel(allData, usbPath, context, true)
|
|
||||||
loadingDialog?.dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun testCopyVideo(
|
|
||||||
context: Context,
|
|
||||||
usbPath: String,
|
|
||||||
datas: MutableList<Video>,
|
|
||||||
loadingDialog: CommonDialog?
|
|
||||||
) {
|
|
||||||
val usbVideoPath = usbPath + File.separator + "Video"
|
|
||||||
val dFile = File(usbVideoPath);
|
|
||||||
if (!dFile.exists() || !dFile.isDirectory) {
|
|
||||||
dFile.mkdir()
|
|
||||||
}
|
|
||||||
datas.forEach {
|
|
||||||
FileUtils.copy(
|
|
||||||
File(it.path).inputStream(),
|
|
||||||
File(usbVideoPath + "/${it.name}").outputStream()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
loadingDialog?.dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getSupportCodec() {
|
|
||||||
val list = MediaCodecList(MediaCodecList.ALL_CODECS)
|
|
||||||
val codecs = list.codecInfos
|
|
||||||
Log.d("cyy", "Decoders:")
|
|
||||||
for (i in codecs.indices) {
|
|
||||||
val codec = codecs[i]
|
|
||||||
Log.d("cyy", codec.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,74 @@
|
|||||||
|
package com.yinuo.safetywatcher.watcher.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.FileUtils
|
||||||
|
import com.common.commonlib.db.entity.Gas
|
||||||
|
import com.common.commonlib.db.entity.Video
|
||||||
|
import com.common.commonlib.db.entity.Warning
|
||||||
|
import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog
|
||||||
|
import com.yinuo.safetywatcher.watcher.xls.SimpleCellValue
|
||||||
|
import com.yinuo.safetywatcher.watcher.xls.utils.ExcelUtils
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
object ExportUtils {
|
||||||
|
fun testExportExcel(
|
||||||
|
context: Context,
|
||||||
|
usbPath: String,
|
||||||
|
datas: MutableList<Gas>,
|
||||||
|
commonDialog: CommonDialog?
|
||||||
|
): String? {
|
||||||
|
val allData = mutableListOf<List<SimpleCellValue>>()
|
||||||
|
datas.forEach {
|
||||||
|
val row = mutableListOf<SimpleCellValue>()
|
||||||
|
row.add(SimpleCellValue(DateUtils.formatHistoryTime(it.time) ?: ""))
|
||||||
|
row.add(SimpleCellValue(it.gasName))
|
||||||
|
row.add(SimpleCellValue(it.gasValue.toString()))
|
||||||
|
row.add(SimpleCellValue(it.unit))
|
||||||
|
allData.add(row)
|
||||||
|
}
|
||||||
|
val excelPath = ExcelUtils.writeStringListToExcel(allData, usbPath, context)
|
||||||
|
commonDialog?.dismiss()
|
||||||
|
return excelPath
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun testExportWarnExcel(
|
||||||
|
context: Context,
|
||||||
|
usbPath: String,
|
||||||
|
datas: MutableList<Warning>,
|
||||||
|
loadingDialog: CommonDialog?
|
||||||
|
): String? {
|
||||||
|
val allData = mutableListOf<List<SimpleCellValue>>()
|
||||||
|
datas.forEach {
|
||||||
|
val row = mutableListOf<SimpleCellValue>()
|
||||||
|
row.add(SimpleCellValue(DateUtils.formatHistoryTime(it.startTime) ?: ""))
|
||||||
|
row.add(SimpleCellValue(it.gasName))
|
||||||
|
row.add(SimpleCellValue(it.gasValue.toString()))
|
||||||
|
row.add(SimpleCellValue(it.unit))
|
||||||
|
allData.add(row)
|
||||||
|
}
|
||||||
|
val excelPath = ExcelUtils.writeStringListToExcel(allData, usbPath, context, true)
|
||||||
|
loadingDialog?.dismiss()
|
||||||
|
return excelPath
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testCopyVideo(
|
||||||
|
context: Context,
|
||||||
|
usbPath: String,
|
||||||
|
datas: MutableList<Video>,
|
||||||
|
loadingDialog: CommonDialog?
|
||||||
|
) {
|
||||||
|
val usbVideoPath = usbPath + File.separator + "Video"
|
||||||
|
val dFile = File(usbVideoPath);
|
||||||
|
if (!dFile.exists() || !dFile.isDirectory) {
|
||||||
|
dFile.mkdir()
|
||||||
|
}
|
||||||
|
datas.forEach {
|
||||||
|
FileUtils.copy(
|
||||||
|
File(it.path).inputStream(),
|
||||||
|
File(usbVideoPath + "/${it.name}").outputStream()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
loadingDialog?.dismiss()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue