forked from upstream-mirrors/OpenTracks
Refactor: introduced SensorConnector interface.
This commit is contained in:
@@ -50,7 +50,7 @@ public class GPSHandlerTest {
|
|||||||
Mockito.when(trackPointCreator.createNow())
|
Mockito.when(trackPointCreator.createNow())
|
||||||
.thenReturn(Instant.now());
|
.thenReturn(Instant.now());
|
||||||
|
|
||||||
locationHandler.onStart(context, new Handler());
|
locationHandler.start(context, new Handler());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import de.dennisguse.opentracks.data.models.TrackPoint;
|
|||||||
/**
|
/**
|
||||||
* Estimates the altitude gain and altitude loss using the device's pressure sensor (i.e., barometer).
|
* Estimates the altitude gain and altitude loss using the device's pressure sensor (i.e., barometer).
|
||||||
*/
|
*/
|
||||||
public class AltitudeSumManager implements SensorEventListener {
|
public class AltitudeSumManager implements SensorConnector, SensorEventListener {
|
||||||
|
|
||||||
private static final String TAG = AltitudeSumManager.class.getSimpleName();
|
private static final String TAG = AltitudeSumManager.class.getSimpleName();
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ import de.dennisguse.opentracks.util.PermissionRequester;
|
|||||||
*
|
*
|
||||||
* @author Sandor Dornbush
|
* @author Sandor Dornbush
|
||||||
*/
|
*/
|
||||||
public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.SensorDataObserver {
|
public class BluetoothRemoteSensorManager implements SensorConnector, BluetoothConnectionManager.SensorDataObserver {
|
||||||
|
|
||||||
private static final String TAG = BluetoothRemoteSensorManager.class.getSimpleName();
|
private static final String TAG = BluetoothRemoteSensorManager.class.getSimpleName();
|
||||||
|
|
||||||
@@ -121,14 +121,16 @@ public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.
|
|||||||
bluetoothAdapter = BluetoothUtils.getAdapter(context);
|
bluetoothAdapter = BluetoothUtils.getAdapter(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
@Override
|
||||||
|
public void start(Context context, Handler handler) {
|
||||||
started = true;
|
started = true;
|
||||||
|
|
||||||
//Registering triggers connection startup
|
//Registering triggers connection startup
|
||||||
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void stop() {
|
@Override
|
||||||
|
public synchronized void stop(Context context) {
|
||||||
heartRate.disconnect();
|
heartRate.disconnect();
|
||||||
cyclingCadence.disconnect();
|
cyclingCadence.disconnect();
|
||||||
cyclingSpeed.disconnect();
|
cyclingSpeed.disconnect();
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package de.dennisguse.opentracks.sensors;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
|
|
||||||
|
public interface SensorConnector {
|
||||||
|
|
||||||
|
void start(Context context, Handler handler);
|
||||||
|
|
||||||
|
void stop(Context context);
|
||||||
|
}
|
||||||
@@ -20,12 +20,13 @@ import java.time.Instant;
|
|||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
import de.dennisguse.opentracks.data.models.Distance;
|
import de.dennisguse.opentracks.data.models.Distance;
|
||||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||||
|
import de.dennisguse.opentracks.sensors.SensorConnector;
|
||||||
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
||||||
import de.dennisguse.opentracks.util.LocationUtils;
|
import de.dennisguse.opentracks.util.LocationUtils;
|
||||||
import de.dennisguse.opentracks.util.PermissionRequester;
|
import de.dennisguse.opentracks.util.PermissionRequester;
|
||||||
|
|
||||||
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
|
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
|
||||||
public class GPSHandler implements LocationListenerCompat, GpsStatus.GpsStatusListener, SharedPreferences.OnSharedPreferenceChangeListener {
|
public class GPSHandler implements SensorConnector, LocationListenerCompat, GpsStatus.GpsStatusListener, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
private final String TAG = GPSHandler.class.getSimpleName();
|
private final String TAG = GPSHandler.class.getSimpleName();
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ public class GPSHandler implements LocationListenerCompat, GpsStatus.GpsStatusLi
|
|||||||
this.trackPointCreator = trackPointCreator;
|
this.trackPointCreator = trackPointCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onStart(@NonNull Context context, @NonNull Handler handler) {
|
public void start(@NonNull Context context, @NonNull Handler handler) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
|
||||||
@@ -61,7 +62,7 @@ public class GPSHandler implements LocationListenerCompat, GpsStatus.GpsStatusLi
|
|||||||
|
|
||||||
@SuppressWarnings({"MissingPermission"})
|
@SuppressWarnings({"MissingPermission"})
|
||||||
//TODO upgrade to AGP7.0.0/API31 started complaining about removeUpdates.
|
//TODO upgrade to AGP7.0.0/API31 started complaining about removeUpdates.
|
||||||
public void onStop() {
|
public void stop(Context context) {
|
||||||
if (locationManager != null && context != null) {
|
if (locationManager != null && context != null) {
|
||||||
if (PermissionRequester.GPS.hasPermission(context)) {
|
if (PermissionRequester.GPS.hasPermission(context)) {
|
||||||
LocationManagerCompat.removeUpdates(locationManager, this);
|
LocationManagerCompat.removeUpdates(locationManager, this);
|
||||||
|
|||||||
@@ -52,12 +52,12 @@ public class TrackPointCreator implements BluetoothRemoteSensorManager.SensorDat
|
|||||||
public synchronized void start(@NonNull Context context, @NonNull Handler handler) {
|
public synchronized void start(@NonNull Context context, @NonNull Handler handler) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
|
||||||
gpsHandler.onStart(context, handler);
|
gpsHandler.start(context, handler);
|
||||||
|
|
||||||
remoteSensorManager = new BluetoothRemoteSensorManager(context, handler, this);
|
remoteSensorManager = new BluetoothRemoteSensorManager(context, handler, this);
|
||||||
altitudeSumManager = new AltitudeSumManager();
|
altitudeSumManager = new AltitudeSumManager();
|
||||||
|
|
||||||
remoteSensorManager.start();
|
remoteSensorManager.start(context, handler);
|
||||||
altitudeSumManager.start(context, handler);
|
altitudeSumManager.start(context, handler);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -94,10 +94,10 @@ public class TrackPointCreator implements BluetoothRemoteSensorManager.SensorDat
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void stop() {
|
public synchronized void stop() {
|
||||||
gpsHandler.onStop();
|
gpsHandler.stop(context);
|
||||||
|
|
||||||
if (remoteSensorManager != null) {
|
if (remoteSensorManager != null) {
|
||||||
remoteSensorManager.stop();
|
remoteSensorManager.stop(context);
|
||||||
remoteSensorManager = null;
|
remoteSensorManager = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user