package com.yinuo.safetywatcher import android.app.Activity import android.content.Context import android.content.Intent import android.net.Uri import android.os.Build import android.provider.Settings import com.common.commonlib.net.bean.BaseResponse import com.common.commonlib.net.callback.RequestResultCallBack import com.common.commonlib.db.DBUtils import com.common.commonlib.db.entity.Gas import com.common.commonlib.db.entity.GasType import com.yinuo.safetywatcher.watcher.net.api.UploadFileApi import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog import com.yinuo.safetywatcher.watcher.utils.DateUtils import com.yinuo.safetywatcher.xls.SimpleCellValue import com.yinuo.safetywatcher.xls.utils.ExcelUtils import com.yinuo.safetywatcher.xls.utils.PathUtils import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch object TestUtils { fun insertData() { GlobalScope.launch() { // 插入类型 val typeDao = DBUtils.gasTypeDao() val all = typeDao.getAll() if (all.isNotEmpty()) { return@launch } val list = mutableListOf() list.add(GasType("CO")) list.add(GasType("CO2")) list.add(GasType("O2")) typeDao.insertAll(list) // 构造气体数据 val timeMillis = System.currentTimeMillis() val gasDao = DBUtils.gasDao() val gases = mutableListOf() gases.add(Gas(timeMillis - 90 * 1000, "CO", 1.0)) gases.add(Gas(timeMillis - 90 * 1000, "O2", 1.0)) gases.add(Gas(timeMillis - 90 * 1000, "CO2", 1.0)) gases.add(Gas(timeMillis - 60 * 1000, "CO2", 1.0)) gases.add(Gas(timeMillis - 60 * 1000, "CO", 1.0)) gases.add(Gas(timeMillis - 60 * 1000, "O2", 1.0)) gases.add(Gas(timeMillis - 30 * 1000, "O2", 1.0)) gases.add(Gas(timeMillis - 30 * 1000, "CO", 1.0)) gases.add(Gas(timeMillis - 30 * 1000, "CO2", 1.0)) gases.add(Gas(timeMillis, "CO", 1.0)) gases.add(Gas(timeMillis, "CO2", 1.0)) gases.add(Gas(timeMillis, "O2", 1.0)) gasDao.insertAll(gases) } } 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, path: String, commonDialog: CommonDialog) { val path = PathUtils.getExternalStorageDirectory() + "/test2.mp4" uploadApi.singleUpload(path, System.currentTimeMillis(), object : RequestResultCallBack() { override fun onResult(result: BaseResponse) { commonDialog.dismiss() } override fun onError(error: String?) { commonDialog.dismiss() } }) } fun testExportExcel(context: Context, datas: MutableList, commonDialog: CommonDialog) { val allData = mutableListOf>() datas.forEach { val row = mutableListOf() 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) } ExcelUtils.writeStringListToExcel(allData, context); commonDialog.dialogBinding?.root?.postDelayed({ commonDialog.dismiss() }, 1000) } }