Skip to content

Commit

Permalink
improve sensor name matching by looking for best match
Browse files Browse the repository at this point in the history
fixes an issue where having both ORIENTATION and DEVICE_ORIENTATION sensors makes it impossible to select ORIENTATION with the -s flag, prevents similar issues in the future
  • Loading branch information
TomoBossi authored Jul 20, 2024
1 parent cff225e commit 44e1852
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions app/src/main/java/com/termux/api/apis/SensorAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,25 @@ protected static List<Sensor> getSensorsToListenTo(SensorManager sensorManager,
} else {

// try to find matching sensors that were sent in request
for (String sensorName : requestedSensors) {
for (String requestedSensorName : requestedSensors) {
// ignore case
sensorName = sensorName.toUpperCase();
requestedSensorName = requestedSensorName.toUpperCase();

Sensor bestMatchingSensor = null;
int matchingSensorNameLength = Integer.MAX_VALUE;

for (Sensor sensor : availableSensors) {
if (sensor.getName().toUpperCase().contains(sensorName)) {
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_UI);
sensorsToListenTo.add(sensor);
break;
String sensorName = sensor.getName().toUpperCase();
if (sensorName.contains(requestedSensorName) && sensorName.length() < matchingSensorNameLength) {
bestMatchingSensor = sensor;
matchingSensorNameLength = sensorName.length();
}
}

if (bestMatchingSensor != null) {
sensorManager.registerListener(sensorEventListener, bestMatchingSensor, SensorManager.SENSOR_DELAY_UI);
sensorsToListenTo.add(bestMatchingSensor);
}
}
}
return sensorsToListenTo;
Expand Down

0 comments on commit 44e1852

Please sign in to comment.