Skip to content

Commit 97b4100

Browse files
Fixed(BatteryStatusAPI): Output double value instead of string for temperature field with value rounded up to 1 decimal place
`String.format("%.1f", batteryTemperature)` was used in d5364ef to round up the value to 1 decimal place, which resulted in field being a string in json output and with a comma-as-decimal-separater depending on device locale, like `25,5` instead of `25.5`, so we stay with double now. Closes #763
1 parent ad2161a commit 97b4100

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

app/src/main/java/com/termux/api/apis/BatteryStatusAPI.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ public void writeJson(JsonWriter out) throws Exception {
8787
batteryPlugged = "PLUGGED_" + pluggedInt;
8888
}
8989

90-
double batteryTemperature = batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) / 10.f;
90+
// Android returns battery temperature as int in tenths of degrees Celsius, like 255, so convert it to a decimal like 25.5°C.
91+
// - https://cs.android.com/android/platform/superproject/+/android-15.0.0_r1:hardware/interfaces/health/aidl/android/hardware/health/HealthInfo.aidl;l=77-80
92+
double batteryTemperature = ((double) batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, Integer.MIN_VALUE)) / 10f;
93+
// Round the value to 1 decimal place.
94+
batteryTemperature = (double) Math.round(batteryTemperature * 10.0f) / 10.0f;
9195

9296
String batteryStatusString;
9397
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
@@ -145,7 +149,7 @@ public void writeJson(JsonWriter out) throws Exception {
145149
out.name("health").value(batteryHealth);
146150
out.name("plugged").value(batteryPlugged);
147151
out.name("status").value(batteryStatusString);
148-
out.name("temperature").value(String.format("%.1f", batteryTemperature));
152+
out.name("temperature").value(batteryTemperature);
149153
out.name("voltage").value(batteryVoltage);
150154
out.name("current").value(batteryCurrentNow);
151155
out.name("current_average").value(getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE));

0 commit comments

Comments
 (0)