Skip to content

Commit 27a7599

Browse files
Fixed: Fix NullPointerException in BatteryStatusAPI while converting a null Integer to an int for batteryCurrentNow and also do not output null values
``` java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com.termux.api.apis.BatteryStatusAPI$1.writeJson(SourceFile:138) at com.termux.api.util.ResultReturner$ResultJsonWriter.writeResult(SourceFile:169) at com.termux.api.util.ResultReturner.lambda$returnData$0(SourceFile:271) at com.termux.api.util.ResultReturner.$r8$lambda$RFR2zSHu5FsJH7JvuCx4CPnUmMY(SourceFile:0) at com.termux.api.util.ResultReturner$$ExternalSyntheticLambda0.run(SourceFile:0) at java.lang.Thread.run(Thread.java:1119) Suppressed: java.lang.Exception: Called by: at com.termux.api.util.ResultReturner.returnData(SourceFile:239) at com.termux.api.apis.BatteryStatusAPI.onReceive(SourceFile:27) at com.termux.api.TermuxApiReceiver.doWork(SourceFile:91) at com.termux.api.TermuxApiReceiver.onReceive(SourceFile:65) at android.app.ActivityThread.handleReceiver(ActivityThread.java:4458) at android.app.ActivityThread.access$1800(ActivityThread.java:277) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2172) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:210) at android.os.Looper.loop(Looper.java:299) at android.app.ActivityThread.main(ActivityThread.java:8298) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073) ```
1 parent 40abe78 commit 27a7599

File tree

2 files changed

+88
-17
lines changed

2 files changed

+88
-17
lines changed

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.termux.api.apis;
22

3+
import static com.termux.api.util.JsonUtils.*;
4+
35
import android.annotation.SuppressLint;
46
import android.content.Context;
57
import android.content.Intent;
@@ -30,6 +32,7 @@ public static void onReceive(TermuxApiReceiver apiReceiver, final Context contex
3032
public void writeJson(JsonWriter out) throws Exception {
3133
// - https://cs.android.com/android/platform/superproject/+/android-15.0.0_r1:frameworks/base/services/core/java/com/android/server/BatteryService.java;l=745
3234
Intent batteryStatus = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
35+
if (batteryStatus == null) batteryStatus = new Intent();
3336

3437
int batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
3538
int batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
@@ -135,38 +138,40 @@ public void writeJson(JsonWriter out) throws Exception {
135138
// charger does not output enough current, and will result in false inversions.
136139
// - https://developer.android.com/reference/android/os/BatteryManager#BATTERY_PROPERTY_CURRENT_NOW
137140
// - https://issuetracker.google.com/issues/37131318
138-
int batteryCurrentNow = getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
141+
Integer batteryCurrentNow = getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
139142

140143
// - https://stackoverflow.com/questions/64532112/batterymanagers-battery-property-current-now-returning-0-or-incorrect-current-v
141-
if (Math.abs(batteryCurrentNow / 1000) < 1.0) {
144+
if (batteryCurrentNow != null && Math.abs(batteryCurrentNow / 1000) < 1.0) {
142145
Logger.logVerbose(LOG_TAG, "Fixing current_now from " + batteryCurrentNow + " to " + (batteryCurrentNow * 1000));
143146
batteryCurrentNow = batteryCurrentNow * 1000;
144147
}
145148

146149
out.beginObject();
147-
out.name("present").value(batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false));
148-
out.name("technology").value(batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY));
149-
out.name("health").value(batteryHealth);
150-
out.name("plugged").value(batteryPlugged);
151-
out.name("status").value(batteryStatusString);
152-
out.name("temperature").value(batteryTemperature);
153-
out.name("voltage").value(batteryVoltage);
154-
out.name("current").value(batteryCurrentNow);
155-
out.name("current_average").value(getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE));
156-
out.name("percentage").value(getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CAPACITY));
157-
out.name("level").value(batteryLevel);
158-
out.name("scale").value(batteryScale);
159-
out.name("charge_counter").value(getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER));
160-
out.name("energy").value(getLongProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER));
150+
putBooleanValueIfSet(out, "present", batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false));
151+
putStringIfSet(out, "technology", batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY));
152+
putStringIfSet(out, "health", batteryHealth);
153+
putStringIfSet(out, "plugged", batteryPlugged);
154+
putStringIfSet(out, "status", batteryStatusString);
155+
putDoubleIfSet(out, "temperature", batteryTemperature);
156+
putIntegerIfSet(out, "voltage", batteryVoltage);
157+
putIntegerIfSet(out, "current", batteryCurrentNow);
158+
putIntegerIfSet(out, "current_average", getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE));
159+
putIntegerIfSet(out, "percentage", getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CAPACITY));
160+
putIntegerIfSet(out, "level", batteryLevel);
161+
putIntegerIfSet(out, "scale", batteryScale);
162+
putIntegerIfSet(out, "charge_counter", getIntProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER));
163+
putLongIfSet(out, "energy", getLongProperty(batteryManager, BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER));
161164
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
162165
int batteryCycle = batteryStatus.getIntExtra(BatteryManager.EXTRA_CYCLE_COUNT, -1);
163-
out.name("cycle").value(batteryCycle != -1 ? batteryCycle : null);
166+
putIntegerIfSet(out, "cycle", batteryCycle != -1 ? batteryCycle : null);
164167
}
165168
out.endObject();
166169
}
167170
});
168171
}
169172

173+
174+
170175
/**
171176
* - https://developer.android.com/reference/android/os/BatteryManager.html#getIntProperty(int)
172177
*/
@@ -187,4 +192,5 @@ private static Long getLongProperty(BatteryManager batteryManager, int id) {
187192
long value = batteryManager.getLongProperty(id);
188193
return value != Long.MIN_VALUE ? value : null;
189194
}
195+
190196
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.termux.api.util;
2+
3+
import android.util.JsonWriter;
4+
5+
import com.termux.shared.logger.Logger;
6+
7+
import java.io.IOException;
8+
9+
public class JsonUtils {
10+
11+
public static final String LOG_TAG = "JsonUtils";
12+
13+
14+
15+
public static void putBooleanValueIfSet(JsonWriter out, String key, Boolean value) {
16+
if (out == null || key == null || key.isEmpty() || value == null) return;
17+
18+
try {
19+
out.name(key).value(value);
20+
} catch (IOException e) {
21+
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to put \"" + key + "\" with boolean value \"" + value + "\"", e);
22+
}
23+
}
24+
25+
public static void putIntegerIfSet(JsonWriter out, String key, Integer value) {
26+
if (out == null || key == null || key.isEmpty() || value == null) return;
27+
28+
try {
29+
out.name(key).value(value);
30+
} catch (IOException e) {
31+
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to put \"" + key + "\" with integer value \"" + value + "\"", e);
32+
}
33+
}
34+
35+
public static void putLongIfSet(JsonWriter out, String key, Long value) {
36+
if (out == null || key == null || key.isEmpty() || value == null) return;
37+
38+
try {
39+
out.name(key).value(value);
40+
} catch (IOException e) {
41+
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to put \"" + key + "\" with long value \"" + value + "\"", e);
42+
}
43+
}
44+
45+
public static void putDoubleIfSet(JsonWriter out, String key, Double value) {
46+
if (out == null || key == null || key.isEmpty() || value == null) return;
47+
48+
try {
49+
out.name(key).value(value);
50+
} catch (IOException e) {
51+
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to put \"" + key + "\" with double value \"" + value + "\"", e);
52+
}
53+
}
54+
55+
public static void putStringIfSet(JsonWriter out, String key, String value) {
56+
if (out == null || key == null || key.isEmpty() || value == null) return;
57+
58+
try {
59+
out.name(key).value(value);
60+
} catch (IOException e) {
61+
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to put \"" + key + "\" with string value \"" + value + "\"", e);
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)