Commit 4b65f1a7 authored by wanglei's avatar wanglei

...

parent d8150eaf
...@@ -3,6 +3,7 @@ package com.base.filerecoveryrecyclebin.activity.battery ...@@ -3,6 +3,7 @@ package com.base.filerecoveryrecyclebin.activity.battery
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.IntentFilter import android.content.IntentFilter
import android.graphics.Color import android.graphics.Color
import android.os.BatteryManager
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
...@@ -21,6 +22,7 @@ import kotlinx.coroutines.launch ...@@ -21,6 +22,7 @@ import kotlinx.coroutines.launch
import java.io.BufferedReader import java.io.BufferedReader
import java.io.FileReader import java.io.FileReader
class BatteryActivity : BaseActivity<ActivityBatteryBinding>() { class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
private val TAG = "BatteryActivity" private val TAG = "BatteryActivity"
...@@ -30,6 +32,8 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() { ...@@ -30,6 +32,8 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
} }
private lateinit var batteryReceiver: BatteryReceiver private lateinit var batteryReceiver: BatteryReceiver
private lateinit var mBatteryManager: BatteryManager
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
override fun initView() { override fun initView() {
BarUtils.setStatusBarLightMode(this, true) BarUtils.setStatusBarLightMode(this, true)
...@@ -43,18 +47,20 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() { ...@@ -43,18 +47,20 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
addAction("android.intent.action.BATTERY_CHANGED") addAction("android.intent.action.BATTERY_CHANGED")
}) })
getAverageCurrent() mBatteryManager = getSystemService(BATTERY_SERVICE) as BatteryManager
lifecycleScope.launch(Dispatchers.Main) { // getAverageCurrent()
currentFlow.collectLatest { //
binding.tvElectric.text = "${it}mA" // lifecycleScope.launch(Dispatchers.Main) {
} // currentFlow.collectLatest {
} // binding.tvElectric.text = "${it}mA"
lifecycleScope.launch(Dispatchers.Main) { // }
averageFlow.collectLatest { // }
binding.tvCurrentAverage.text = "${it}mA" // lifecycleScope.launch(Dispatchers.Main) {
} // averageFlow.collectLatest {
} // binding.tvCurrentAverage.text = "${it}mA"
// }
// }
AdmobMaxHelper.admobMaxShowNativeAd(this, binding.flAd, 2) AdmobMaxHelper.admobMaxShowNativeAd(this, binding.flAd, 2)
} }
...@@ -62,81 +68,91 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() { ...@@ -62,81 +68,91 @@ class BatteryActivity : BaseActivity<ActivityBatteryBinding>() {
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun updateUi() { private fun updateUi() {
val temperature = BatteryReceiver.temperature / 10f val temperature = BatteryReceiver.temperature / 10f
binding.tvTemperature.text = "%.1f °C".format(temperature) binding.tvTemperature.text = format(temperature)
binding.tvVoltage.text = "${BatteryReceiver.voltage}mV" binding.tvVoltage.text = "${format((BatteryReceiver.voltage / 1000f))} mV"
binding.tvTechnology.text = BatteryReceiver.technology binding.tvTechnology.text = BatteryReceiver.technology
binding.tvCapacity.text = "${BatteryReceiver.mAh.toInt()}mAh" binding.tvCapacity.text = "${BatteryReceiver.mAh.toInt()} mAh"
}
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(batteryReceiver)
}
val currentNow = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
binding.tvElectric.text = "${currentNow} mA"
@SuppressLint("PrivateApi") val currentAverage: Int = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)
fun getBatteryFilePath(): String { binding.tvCurrentAverage.text = "${currentAverage} mA"
val systemProperties = Class.forName("android.os.SystemProperties")
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 if (platName.startsWith("exynos"))
"/sys/class/power_supply/battery/BatteryAverageCurrent"
else {
""
} }
LogEx.logDebug(TAG, "filePath=$filePath")
return filePath
}
var mAverageFlow = MutableSharedFlow<Float>()
val averageFlow: SharedFlow<Float> = mAverageFlow
var mCurrentFlow = MutableSharedFlow<Int>() fun format(float: Float): String {
var currentFlow: SharedFlow<Int> = mCurrentFlow return "%.1f °C".format(float)
private fun getAverageCurrent(filePath: String = getBatteryFilePath()) {
var sum = 0
var loopCount = 0
lifecycleScope.launch(Dispatchers.IO) {
while (isActive) {
val currentValue = readFileValue(filePath)
mCurrentFlow.emit(currentValue)
sum += currentValue
loopCount++
val average = sum / loopCount.toFloat()
mAverageFlow.emit(average)
delay(150)
}
} }
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(batteryReceiver)
} }
private fun readFileValue(filePath: String): Int { // @SuppressLint("PrivateApi")
var value = 0 // fun getBatteryFilePath(): String {
var br: BufferedReader? = null // val systemProperties = Class.forName("android.os.SystemProperties")
try { // val getMethod = systemProperties.getDeclaredMethod("get", String::class.java)
br = BufferedReader(FileReader(filePath)) // val platName = getMethod.invoke(null, "ro.hardware") as String
val line = br.readLine() //
value = line?.toInt() ?: 0 // LogEx.logDebug(TAG, "platName=$platName")
LogEx.logDebug(TAG, "readFileValue value=$value") // val filePath: String = if (platName.startsWith("mt") || platName.startsWith("MT")) {
} catch (e: Exception) { // "/sys/class/power_supply/battery/device/FG_Battery_CurrentConsumption"
e.printStackTrace() // // MTK平台该值不区分充放电,都为负数
} finally { // } else if (platName.startsWith("qcom")) {
br?.close() // "/sys/class/power_supply/battery/current_now"
} // } else if (platName.startsWith("exynos"))
return value // "/sys/class/power_supply/battery/BatteryAverageCurrent"
} // else {
// ""
// }
// LogEx.logDebug(TAG, "filePath=$filePath")
// return filePath
// }
// var mAverageFlow = MutableSharedFlow<Float>()
// val averageFlow: SharedFlow<Float> = mAverageFlow
//
// var mCurrentFlow = MutableSharedFlow<Int>()
// var currentFlow: SharedFlow<Int> = mCurrentFlow
// private fun getAverageCurrent(filePath: String = getBatteryFilePath()) {
// var sum = 0
// var loopCount = 0
// lifecycleScope.launch(Dispatchers.IO) {
// while (isActive) {
// val currentValue = readFileValue(filePath)
// mCurrentFlow.emit(currentValue)
// sum += currentValue
// loopCount++
// val average = sum / loopCount.toFloat()
// mAverageFlow.emit(average)
// delay(150)
// }
// }
// }
// private fun readFileValue(filePath: String): Int {
// var value = 0
// var br: BufferedReader? = null
// try {
// br = BufferedReader(FileReader(filePath))
// val line = br.readLine()
// value = line?.toInt() ?: 0
// LogEx.logDebug(TAG, "readFileValue value=$value")
// } catch (e: Exception) {
// e.printStackTrace()
// } finally {
// br?.close()
// }
// return value
// }
} }
\ No newline at end of file
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