Skip to content

Commit 510ccdc

Browse files
AsCressmarcnause
authored andcommitted
fix: fixed crash in PowerSourceActivity by adding checks to input values
1 parent 8ed0ca5 commit 510ccdc

File tree

1 file changed

+66
-36
lines changed

1 file changed

+66
-36
lines changed

app/src/main/java/io/pslab/activity/PowerSourceActivity.java

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.sdsmdg.harjot.crollerTest.Croller;
3030
import com.sdsmdg.harjot.crollerTest.OnCrollerChangeListener;
3131

32+
import org.apache.commons.lang3.math.NumberUtils;
33+
3234
import java.util.Date;
3335
import java.util.Locale;
3436
import java.util.Timer;
@@ -190,17 +192,24 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
190192
if (actionId == EditorInfo.IME_ACTION_DONE) {
191193
String voltageValue = displayPV1.getText().toString();
192194
voltageValue = voltageValue.replace("V", "");
193-
float voltage = Float.parseFloat(voltageValue);
194-
if (voltage < -5.00f) {
195-
voltage = -5.00f;
196-
displayPV1.setText(voltage + " V");
197-
}
198-
if (voltage > 5.00f) {
199-
voltage = 5.00f;
195+
if (NumberUtils.isCreatable(voltageValue)) {
196+
float voltage = Float.parseFloat(voltageValue);
197+
if (voltage < -5.00f) {
198+
voltage = -5.00f;
199+
displayPV1.setText(voltage + " V");
200+
}
201+
if (voltage > 5.00f) {
202+
voltage = 5.00f;
203+
displayPV1.setText(voltage + " V");
204+
}
205+
controllerPV1.setProgress(mapPowerToProgress(voltage, PV1_CONTROLLER_MAX,
206+
5.00f, -5.00f));
207+
} else {
208+
float voltage = -5.00f;
200209
displayPV1.setText(voltage + " V");
210+
controllerPV1.setProgress(mapPowerToProgress(voltage, PV1_CONTROLLER_MAX,
211+
5.00f, -5.00f));
201212
}
202-
controllerPV1.setProgress(mapPowerToProgress(voltage, PV1_CONTROLLER_MAX,
203-
5.00f, -5.00f));
204213
}
205214
return false;
206215
}
@@ -212,17 +221,24 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
212221
if (actionId == EditorInfo.IME_ACTION_DONE) {
213222
String voltageValue = displayPV2.getText().toString();
214223
voltageValue = voltageValue.replace("V", "");
215-
float voltage = Float.parseFloat(voltageValue);
216-
if (voltage < -3.30f) {
217-
voltage = -3.30f;
218-
displayPV2.setText(voltage + " V");
219-
}
220-
if (voltage > 3.30f) {
221-
voltage = 3.30f;
224+
if (NumberUtils.isCreatable(voltageValue)) {
225+
float voltage = Float.parseFloat(voltageValue);
226+
if (voltage < -3.30f) {
227+
voltage = -3.30f;
228+
displayPV2.setText(voltage + " V");
229+
}
230+
if (voltage > 3.30f) {
231+
voltage = 3.30f;
232+
displayPV2.setText(voltage + " V");
233+
}
234+
controllerPV2.setProgress(mapPowerToProgress(voltage, PV2_CONTROLLER_MAX,
235+
3.30f, -3.30f));
236+
} else {
237+
float voltage = -3.30f;
222238
displayPV2.setText(voltage + " V");
239+
controllerPV2.setProgress(mapPowerToProgress(voltage, PV2_CONTROLLER_MAX,
240+
3.30f, -3.30f));
223241
}
224-
controllerPV2.setProgress(mapPowerToProgress(voltage, PV2_CONTROLLER_MAX,
225-
3.30f, -3.30f));
226242
}
227243
return false;
228244
}
@@ -234,17 +250,24 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
234250
if (actionId == EditorInfo.IME_ACTION_DONE) {
235251
String voltageValue = displayPV3.getText().toString();
236252
voltageValue = voltageValue.replace("V", "");
237-
float voltage = Float.parseFloat(voltageValue);
238-
if (voltage < 0.00f) {
239-
voltage = 0.00f;
240-
displayPV3.setText(voltage + " V");
241-
}
242-
if (voltage > 3.30f) {
243-
voltage = 3.30f;
253+
if (NumberUtils.isCreatable(voltageValue)) {
254+
float voltage = Float.parseFloat(voltageValue);
255+
if (voltage < 0.00f) {
256+
voltage = 0.00f;
257+
displayPV3.setText(voltage + " V");
258+
}
259+
if (voltage > 3.30f) {
260+
voltage = 3.30f;
261+
displayPV3.setText(voltage + " V");
262+
}
263+
controllerPV3.setProgress(mapPowerToProgress(voltage, PV3_CONTROLLER_MAX,
264+
3.30f, 0.00f));
265+
} else {
266+
float voltage = 0.00f;
244267
displayPV3.setText(voltage + " V");
268+
controllerPV3.setProgress(mapPowerToProgress(voltage, PV3_CONTROLLER_MAX,
269+
3.30f, 0.00f));
245270
}
246-
controllerPV3.setProgress(mapPowerToProgress(voltage, PV3_CONTROLLER_MAX,
247-
3.30f, 0.00f));
248271
}
249272
return false;
250273
}
@@ -256,17 +279,24 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
256279
if (actionId == EditorInfo.IME_ACTION_DONE) {
257280
String currentValue = displayPCS.getText().toString();
258281
currentValue = currentValue.replace("mA", "");
259-
float current = Float.parseFloat(currentValue);
260-
if (current < 0.00f) {
261-
current = 0.00f;
262-
displayPCS.setText(current + " mA");
263-
}
264-
if (current > 3.30f) {
265-
current = 3.30f;
282+
if (NumberUtils.isCreatable(currentValue)) {
283+
float current = Float.parseFloat(currentValue);
284+
if (current < 0.00f) {
285+
current = 0.00f;
286+
displayPCS.setText(current + " mA");
287+
}
288+
if (current > 3.30f) {
289+
current = 3.30f;
290+
displayPCS.setText(current + " mA");
291+
}
292+
controllerPCS.setProgress(mapPowerToProgress(current, PCS_CONTROLLER_MAX,
293+
3.30f, 0.00f));
294+
} else {
295+
float current = 0.00f;
266296
displayPCS.setText(current + " mA");
297+
controllerPCS.setProgress(mapPowerToProgress(current, PCS_CONTROLLER_MAX,
298+
3.30f, 0.00f));
267299
}
268-
controllerPCS.setProgress(mapPowerToProgress(current, PCS_CONTROLLER_MAX,
269-
3.30f, 0.00f));
270300
}
271301
return false;
272302
}

0 commit comments

Comments
 (0)