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