Sensor: on start register GPS only once.

Part of #2369.
This commit is contained in:
Dennis Guse
2026-08-17 15:48:21 +02:00
parent d3ff2061c6
commit ce26e20571
4 changed files with 28 additions and 12 deletions
@@ -45,8 +45,9 @@ public class GpsHandler extends SensorHandler<Location, Position> {
gpsStatusManager.onMinSamplingIntervalChanged(PreferencesUtils.getMinSamplingInterval());
}
//TODO
connect();
if (driver != null) {
driver.updateDriverParameters(getContext(), sensorManager.getHandler());
}
}
};
@@ -21,11 +21,11 @@ public abstract class SensorHandler<AggregatorInput, AggregatorOutput> implement
private static final String TAG = SensorHandler.class.getSimpleName();
private final SensorManager sensorManager;
@VisibleForTesting
public Driver driver;
protected final SensorManager sensorManager;
protected Aggregator<AggregatorInput, AggregatorOutput> aggregator;
protected SensorHandler(SensorManager sensorManager) {
@@ -11,5 +11,9 @@ public interface Driver {
//TODO Remove from interface?
boolean isConnected();
default void updateDriverParameters(Context context, Handler handler) {
throw new UnsupportedOperationException();
}
void disconnect();
}
@@ -57,16 +57,11 @@ public class GpsInternal implements Driver {
return;
}
LocationRequestCompat locationRequest = new LocationRequestCompat.Builder(PreferencesUtils.getMinSamplingInterval().toMillis())
.setQuality(LocationRequestCompat.QUALITY_HIGH_ACCURACY)
.setMaxUpdateDelayMillis(0)
.build();
if (PermissionRequester.GPS.hasPermission(context)) {
try {
Log.i(TAG, "Register for location updates " + context);
Log.i(TAG, "Register for location updates");
driverObserver.onConnected(null, null);
LocationManagerCompat.requestLocationUpdates(locationManager, LOCATION_PROVIDER, locationRequest, handler::post, locationListenerCompat);
LocationManagerCompat.requestLocationUpdates(locationManager, LOCATION_PROVIDER, createLocationRequest(), handler::post, locationListenerCompat);
return;
} catch (SecurityException e) {
Log.e(TAG, "Could not register location listener; permissions not granted.", e);
@@ -76,6 +71,16 @@ public class GpsInternal implements Driver {
driverObserver.onDisconnected();
}
@SuppressWarnings({"MissingPermission"})
@Override
public void updateDriverParameters(Context context, Handler handler) {
if (!isConnected()) return;
Log.i(TAG, "Update for location updates");
driverObserver.onConnected(null, null);
LocationManagerCompat.requestLocationUpdates(locationManager, LOCATION_PROVIDER, createLocationRequest(), handler::post, locationListenerCompat);
}
@SuppressWarnings({"MissingPermission"})
@Override
public void disconnect() {
@@ -87,9 +92,15 @@ public class GpsInternal implements Driver {
driverObserver.onDisconnected();
}
@Override
public boolean isConnected() {
return locationManager != null;
}
private LocationRequestCompat createLocationRequest() {
return new LocationRequestCompat.Builder(PreferencesUtils.getMinSamplingInterval().toMillis())
.setQuality(LocationRequestCompat.QUALITY_HIGH_ACCURACY)
.setMaxUpdateDelayMillis(0)
.build();
}
}