SensorManager and GPSManager: prefer to crash rather than hide usage errors.

This commit is contained in:
Dennis Guse
2023-08-10 19:33:00 +02:00
parent ce0365c2e9
commit 205c5c7528
2 changed files with 16 additions and 43 deletions
@@ -62,20 +62,14 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
}
public void stop(Context context) {
if (bluetoothSensorManager != null) {
bluetoothSensorManager.stop(context);
bluetoothSensorManager = null;
}
bluetoothSensorManager.stop(context);
bluetoothSensorManager = null;
if (altitudeSumManager != null) {
altitudeSumManager.stop(context);
altitudeSumManager = null;
}
altitudeSumManager.stop(context);
altitudeSumManager = null;
if (gpsManager != null) {
gpsManager.stop(context);
gpsManager = null;
}
gpsManager.stop(context);
gpsManager = null;
sensorDataSet.clear();
}
@@ -61,9 +61,8 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
}
@SuppressWarnings({"MissingPermission"})
//TODO upgrade to AGP7.0.0/API31 started complaining about removeUpdates.
public void stop(Context context) {
if (locationManager != null) {
if (isStarted()) {
LocationManagerCompat.removeUpdates(locationManager, this);
}
locationManager = null;
@@ -99,7 +98,7 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
}
}
if (registerListener) {
if (registerListener && isStarted()) {
registerLocationListener();
}
}
@@ -111,16 +110,9 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
*/
@Override
public void onLocationChanged(@NonNull Location location) {
if (!isStarted()) {
Log.w(TAG, "Location is ignored; not started.");
return;
}
if (gpsStatusManager != null) {
// Send each update to the status; please note that this TrackPoint is not stored.
TrackPoint trackPoint = new TrackPoint(location, trackPointCreator.createNow());
gpsStatusManager.onNewTrackPoint(trackPoint);
}
// Send each update to the status; please note that this TrackPoint is not stored.
TrackPoint trackPoint = new TrackPoint(location, trackPointCreator.createNow());
gpsStatusManager.onNewTrackPoint(trackPoint);
if (!LocationUtils.isValidLocation(location)) {
Log.w(TAG, "Ignore newTrackPoint. location is invalid.");
@@ -136,29 +128,20 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
public void onStatusChanged(@NonNull String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
if (gpsStatusManager != null) {
gpsStatusManager.onGpsEnabled();
}
gpsStatusManager.onGpsEnabled();
}
@Override
public void onProviderDisabled(@NonNull String provider) {
if (gpsStatusManager != null) {
gpsStatusManager.onGpsDisabled();
}
gpsStatusManager.onGpsDisabled();
}
private void registerLocationListener() {
if (locationManager == null || context == null) {
Log.e(TAG, "Not started.");
return;
}
if (!LocationManagerCompat.hasProvider(locationManager, LOCATION_PROVIDER)) {
Log.e(TAG, "Device doesn't have GPS.");
return;
@@ -171,18 +154,14 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
if (PermissionRequester.GPS.hasPermission(context)) {
try {
final Handler HANDLER = handler;
LocationManagerCompat.requestLocationUpdates(locationManager, LOCATION_PROVIDER, locationRequest, HANDLER::post, this);
Log.i(TAG, "Register for location updates " + context);
LocationManagerCompat.requestLocationUpdates(locationManager, LOCATION_PROVIDER, locationRequest, handler::post, this);
} catch (SecurityException e) {
Log.e(TAG, "Could not register location listener; permissions not granted.", e);
}
}
}
Distance getThresholdHorizontalAccuracy() {
return thresholdHorizontalAccuracy;
}
@Override
public void onGpsStatusChanged(GpsStatusValue currentStatus) {
trackPointCreator.sendGpsStatus(currentStatus);