Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sensor name matching by looking for best match #695

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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