Commit d8150eaf authored by wanglei's avatar wanglei

...

parent c2ee5c97
......@@ -10,6 +10,7 @@ import com.base.filerecoveryrecyclebin.databinding.ActivityBatteryBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.receiver.BatteryReceiver
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.utils.LogEx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
......@@ -19,10 +20,11 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
private val TAG = "BatteryActivity"
override val binding: ActivityBatteryBinding by lazy {
ActivityBatteryBinding.inflate(layoutInflater)
}
......@@ -45,12 +47,12 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
lifecycleScope.launch(Dispatchers.Main) {
currentFlow.collectLatest {
binding.tvElectric.text = "${currentFlow}mA"
binding.tvElectric.text = "${it}mA"
}
}
lifecycleScope.launch(Dispatchers.Main) {
averageFlow.collectLatest {
binding.tvCurrentAverage.text = "${averageFlow}mA"
binding.tvCurrentAverage.text = "${it}mA"
}
}
......@@ -81,14 +83,18 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
val getMethod = systemProperties.getDeclaredMethod("get", String::class.java)
val platName = getMethod.invoke(null, "ro.hardware") as String
LogEx.logDebug(TAG, "platName=$platName")
val filePath: String = if (platName.startsWith("mt") || platName.startsWith("MT")) {
"/sys/class/power_supply/battery/device/FG_Battery_CurrentConsumption"
// MTK平台该值不区分充放电,都为负数
} else if (platName.startsWith("qcom")) {
"/sys/class/power_supply/battery/current_now"
} else {
} else if (platName.startsWith("exynos"))
"/sys/class/power_supply/battery/BatteryAverageCurrent"
else {
""
}
LogEx.logDebug(TAG, "filePath=$filePath")
return filePath
}
......@@ -105,13 +111,13 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
var loopCount = 0
lifecycleScope.launch(Dispatchers.IO) {
while (isActive) {
delay(1500)
val currentValue = readFileValue(filePath)
mCurrentFlow.emit(currentValue)
sum += currentValue
loopCount++
val average = sum / loopCount.toFloat()
mAverageFlow.emit(average)
delay(150)
}
}
}
......@@ -119,11 +125,16 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
private fun readFileValue(filePath: String): Int {
var value = 0
runCatching {
val br = BufferedReader(FileReader(filePath))
var br: BufferedReader? = null
try {
br = BufferedReader(FileReader(filePath))
val line = br.readLine()
br.close()
value = line?.toInt() ?: 0
LogEx.logDebug(TAG, "readFileValue value=$value")
} catch (e: Exception) {
e.printStackTrace()
} finally {
br?.close()
}
return value
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment