这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit d5da717

Browse files
TomoBossiagnostic-apollo
authored andcommitted
Fixed(SensorAPI): Use shortest matched sensor
Previously, one sensor name is a substring of another sensor name (like `ORIENTATION` and `DEVICE_ORIENTATION`), then the first matched sensor in available sensors list would get registered, depending on order of sensors. Now we use the shortest match for the requested sensor in the available sensors list, the shortest being an exact match if requested sensor is passed exactly (ignoring case). Closes #570
1 parent 3f74300 commit d5da717

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,25 @@ protected static List<Sensor> getSensorsToListenTo(SensorManager sensorManager,
287287
} else {
288288

289289
// try to find matching sensors that were sent in request
290-
for (String sensorName : requestedSensors) {
290+
for (String requestedSensor : requestedSensors) {
291291
// ignore case
292-
sensorName = sensorName.toUpperCase();
292+
requestedSensor = requestedSensor.toUpperCase();
293293

294-
for (Sensor sensor : availableSensors) {
295-
if (sensor.getName().toUpperCase().contains(sensorName)) {
296-
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_UI);
297-
sensorsToListenTo.add(sensor);
298-
break;
294+
Sensor shortestMatchSensor = null;
295+
int shortestMatchSensorLength = Integer.MAX_VALUE;
296+
297+
for (Sensor availableSensor : availableSensors) {
298+
String sensorName = availableSensor.getName().toUpperCase();
299+
if (sensorName.contains(requestedSensor) && sensorName.length() < shortestMatchSensorLength) {
300+
shortestMatchSensor = availableSensor;
301+
shortestMatchSensorLength = sensorName.length();
299302
}
300303
}
304+
305+
if (shortestMatchSensor != null) {
306+
sensorManager.registerListener(sensorEventListener, shortestMatchSensor, SensorManager.SENSOR_DELAY_UI);
307+
sensorsToListenTo.add(shortestMatchSensor);
308+
}
301309
}
302310
}
303311
return sensorsToListenTo;

0 commit comments

Comments
 (0)