Skip to content

Commit

Permalink
优化 状态栏充电信息 对于 mA,过大时格式会改变
Browse files Browse the repository at this point in the history
  • Loading branch information
hosizoraru committed Jun 15, 2023
1 parent 34d5d12 commit 858d81f
Showing 1 changed file with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,26 @@ object StatusBarBattery : HookRegister() {
class BatteryReceiver : BroadcastReceiver() {
@SuppressLint("SetTextI18n")
override fun onReceive(context: Context, intent: Intent) {
val any = getBoolean("show_status_bar_battery_any", false)
val mA = getBoolean("current_mA", false)
val current: Double
val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
val temperature = (intent.getIntExtra("temperature", 0) / 10.0)
current = if (mA) {
abs(batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW) / 1000.0)
} else {
abs(batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW) / 1000 / 1000.0)
}
val temperature = intent.getIntExtra("temperature", 0) / 10.0
val batteryCurrentNow =
batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
.toDouble() / 1_000_000

val mA = getBoolean("current_mA", false)
val current = abs(if (mA) batteryCurrentNow * 1000 else batteryCurrentNow)

val currentFormat = if (current >= 1000 || !mA) "%.0f" else "%.2f"
val unit = if (mA) "mA" else "A"

val any = getBoolean("show_status_bar_battery_any", false)
val status = intent.getIntExtra("status", 0)
textview?.run {
text = when {
any || status == BatteryManager.BATTERY_STATUS_CHARGING -> if (mA) {
"${"%.0f".format(current)}mA\n${"%.1f".format(temperature)}"
} else {
"${"%.2f".format(current)}A\n${"%.1f".format(temperature)}"
}
val isVisible = any || status == BatteryManager.BATTERY_STATUS_CHARGING

else -> text
}
visibility = when {
any || status == BatteryManager.BATTERY_STATUS_CHARGING -> View.VISIBLE
else -> View.GONE
textview?.apply {
visibility = if (isVisible) View.VISIBLE else View.GONE
if (isVisible) {
text = "$currentFormat $unit\n%.1f℃".format(current, temperature)
}
}
}
Expand Down

0 comments on commit 858d81f

Please sign in to comment.