Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #1665

Merged
merged 7 commits into from
Aug 13, 2023
Merged

Cleanup #1665

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ captures/
.idea/.name
.idea/caches/
.idea/compiler.xml
.idea/androidTestResultsUserPreferences.xml
.idea/copyright/profiles_settings.xml
.idea/dataSources.ids
.idea/datasources.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import de.dennisguse.opentracks.sensors.sensorData.SensorDataHeartRate;
import de.dennisguse.opentracks.sensors.sensorData.SensorDataSet;
import de.dennisguse.opentracks.services.TrackRecordingService;
import de.dennisguse.opentracks.services.TrackRecordingServiceTestUtils;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
import de.dennisguse.opentracks.stats.TrackStatistics;

Expand Down Expand Up @@ -113,14 +112,10 @@ public static void preSetUp() {

@Before
public void fileSetup() throws IOException {
TrackRecordingServiceTestUtils.resetService(mServiceRule, context);

tmpFile = File.createTempFile("test", "test", context.getFilesDir());
tmpFileUri = Uri.fromFile(tmpFile);

trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(200), true);

TrackRecordingServiceTestUtils.resetService(mServiceRule, context);
}

@After
Expand All @@ -130,8 +125,6 @@ public void tearDown() {

// Ensure that the database is empty after every test
contentProviderUtils.deleteAllTracks(context);

TrackRecordingServiceTestUtils.resetService(mServiceRule, context);
}

public void setUp() throws TimeoutException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ public void setUp() throws TimeoutException {

@After
public void tearDown() {
TrackRecordingServiceTestUtils.resetService(mServiceRule, context);

// Ensure that the database is empty after every test
contentProviderUtils.deleteAllTracks(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import androidx.test.filters.MediumTest;
import androidx.test.rule.ServiceTestRule;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -87,21 +86,13 @@ private TrackRecordingService startService() throws TimeoutException {
@Before
public void setUp() throws TimeoutException {
contentProviderUtils = new ContentProviderUtils(context);
tearDown();

PreferencesUtils.setString(R.string.recording_distance_interval_key, R.string.recording_distance_interval_default);
PreferencesUtils.setString(R.string.idle_speed_key, R.string.idle_speed_default);

service = startService();
}

@After
public void tearDown() {
TrackRecordingServiceTestUtils.resetService(mServiceRule, context);
// Ensure that the database is empty after every test
contentProviderUtils.deleteAllTracks(context);
}

@MediumTest
@Test
public void recording_startStop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public void setUp() throws TimeoutException {

@After
public void tearDown() {
TrackRecordingServiceTestUtils.resetService(mServiceRule, context);
// Ensure that the database is empty after every test
contentProviderUtils.deleteAllTracks(context);
}
Expand Down Expand Up @@ -132,7 +131,7 @@ public void gps_startStop() throws InterruptedException {
assertEquals(GpsStatusValue.GPS_ENABLED, service.getGpsStatusObservable().getValue());

// when
service.stopSensorsAndShutdown();
service.stopSensors();
Thread.sleep(1000);

// then
Expand Down Expand Up @@ -177,7 +176,7 @@ public void recording_startStopResume_no_data() throws InterruptedException {
// then
assertFalse(service.isRecording());
assertEquals(TrackRecordingService.STATUS_DEFAULT, service.getRecordingStatusObservable().getValue());
assertEquals(TrackRecordingService.NOT_RECORDING, service.getRecordingDataObservable().getValue());
assertNotEquals(TrackRecordingService.NOT_RECORDING, service.getRecordingDataObservable().getValue());
assertEquals(GpsStatusValue.GPS_NONE, service.getGpsStatusObservable().getValue());


Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
package de.dennisguse.opentracks.services;

import android.content.Context;
import android.location.Location;

import androidx.test.rule.ServiceTestRule;

import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
import de.dennisguse.opentracks.settings.PreferencesUtils;

public class TrackRecordingServiceTestUtils {


//TODO Workaround as service is not stopped on API23; thus sharedpreferences are not reset between tests.
//TODO Anyhow, the service should re-create all it's resources if a recording starts and makes sure that there is no leftovers from previous recordings.
@Deprecated
public static void resetService(ServiceTestRule mServiceRule, Context context) {
// Let's use default values.
PreferencesUtils.clear();
}

static void sendGPSLocation(TrackPointCreator trackPointCreator, String time, double latitude, double longitude, float accuracy, long speed) {
Location location = new Location("mock");
location.setTime(1L); // Should be ignored anyhow.
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/de/dennisguse/opentracks/TrackListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,10 @@ protected void onCreate(Bundle savedInstanceState) {
if (locationManager != null && !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
} else {
// Invoke trackRecordingService
if (!gpsStatusValue.isGpsStarted()) {
trackRecordingServiceConnection.startAndBindWithCallback(this);
} else {
TrackRecordingService trackRecordingService = trackRecordingServiceConnection.getServiceIfBound();
if (trackRecordingService != null) {
trackRecordingService.stopSensorsAndShutdown(); //TODO Handle this in TrackRecordingServiceConnection
}
if (gpsStatusValue.isGpsStarted()) {
trackRecordingServiceConnection.unbindAndStop(this);
} else {
trackRecordingServiceConnection.startAndBindWithCallback(this);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*
* @author Sandor Dornbush
*/
public class BluetoothRemoteSensorManager implements SensorConnector, AbstractBluetoothConnectionManager.SensorDataObserver {
public class BluetoothRemoteSensorManager implements SensorConnector, AbstractBluetoothConnectionManager.SensorDataObserver, SharedPreferences.OnSharedPreferenceChangeListener {

private static final String TAG = BluetoothRemoteSensorManager.class.getSimpleName();

Expand All @@ -65,39 +65,6 @@ public class BluetoothRemoteSensorManager implements SensorConnector, AbstractBl
private final BluetoothConnectionManagerCyclingPower cyclingPower = new BluetoothConnectionManagerCyclingPower(this);
private final BluetoothConnectionRunningSpeedAndCadence runningSpeedAndCadence = new BluetoothConnectionRunningSpeedAndCadence(this);

private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (sharedPreferences, key) -> {
if (!started) return;

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_heart_rate_key, key)) {
String address = PreferencesUtils.getBluetoothHeartRateSensorAddress();
connect(heartRate, address);
}

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_cadence_key, key)) {
String address = PreferencesUtils.getBluetoothCyclingCadenceSensorAddress();
connect(cyclingCadence, address);
}

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_speed_key, key)) {
String address = PreferencesUtils.getBluetoothCyclingSpeedSensorAddress();

connect(cyclingSpeed, address);
}

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_power_key, key)) {
String address = PreferencesUtils.getBluetoothCyclingPowerSensorAddress();

connect(cyclingPower, address);
}


if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_running_speed_and_cadence_key, key)) {
String address = PreferencesUtils.getBluetoothRunningSpeedAndCadenceAddress();

connect(runningSpeedAndCadence, address);
}
};

public BluetoothRemoteSensorManager(@NonNull Context context, @NonNull Handler handler, @Nullable SensorManager.SensorDataChangedObserver observer) {
this.context = context;
this.handler = handler;
Expand All @@ -109,8 +76,8 @@ public BluetoothRemoteSensorManager(@NonNull Context context, @NonNull Handler h
public void start(Context context, Handler handler) {
started = true;

//Registering triggers connection startup
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
// Triggers connection startup
onSharedPreferenceChanged(null, null);
}

@Override
Expand All @@ -121,7 +88,6 @@ public synchronized void stop(Context context) {
cyclingPower.disconnect();
runningSpeedAndCadence.disconnect();

PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
started = false;
}

Expand Down Expand Up @@ -175,4 +141,37 @@ public void onDisconnecting(SensorData<?> sensorData) {
public Handler getHandler() {
return handler;
}

@Override
public void onSharedPreferenceChanged(SharedPreferences unused, @Nullable String key) {
if (!started) return;

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_heart_rate_key, key)) {
String address = PreferencesUtils.getBluetoothHeartRateSensorAddress();
connect(heartRate, address);
}

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_cadence_key, key)) {
String address = PreferencesUtils.getBluetoothCyclingCadenceSensorAddress();
connect(cyclingCadence, address);
}

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_speed_key, key)) {
String address = PreferencesUtils.getBluetoothCyclingSpeedSensorAddress();

connect(cyclingSpeed, address);
}

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_power_key, key)) {
String address = PreferencesUtils.getBluetoothCyclingPowerSensorAddress();

connect(cyclingPower, address);
}

if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_running_speed_and_cadence_key, key)) {
String address = PreferencesUtils.getBluetoothRunningSpeedAndCadenceAddress();

connect(runningSpeedAndCadence, address);
}
}
}
35 changes: 20 additions & 15 deletions src/main/java/de/dennisguse/opentracks/sensors/SensorManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package de.dennisguse.opentracks.sensors;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import de.dennisguse.opentracks.data.models.TrackPoint;
Expand All @@ -12,7 +14,7 @@
import de.dennisguse.opentracks.services.handlers.GPSManager;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;

public class SensorManager {
public class SensorManager implements SharedPreferences.OnSharedPreferenceChangeListener {

private static final String TAG = SensorManager.class.getSimpleName();

Expand Down Expand Up @@ -49,30 +51,25 @@ public SensorManager(TrackPointCreator observer) {

public void start(Context context, Handler handler) {
gpsManager = new GPSManager(observer); //TODO Pass listener
gpsManager.start(context, handler);

altitudeSumManager = new AltitudeSumManager();
bluetoothSensorManager = new BluetoothRemoteSensorManager(context, handler, listener);

onSharedPreferenceChanged(null, null);

gpsManager.start(context, handler);
altitudeSumManager.start(context, handler);
bluetoothSensorManager.start(context, handler);
}

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();
}
Expand Down Expand Up @@ -114,6 +111,14 @@ public void setAltitudeSumManager(AltitudeSumManager altitudeSumManager) {
this.altitudeSumManager = altitudeSumManager;
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) {
if (gpsManager != null) {
gpsManager.onSharedPreferenceChanged(sharedPreferences, key);
bluetoothSensorManager.onSharedPreferenceChanged(sharedPreferences, key);
}
}

public interface SensorDataChangedObserver {
void onChange(SensorData<?> sensorData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ class TrackRecordingManager implements SharedPreferences.OnSharedPreferenceChang
contentProviderUtils = new ContentProviderUtils(context);
}

public void start() {
PreferencesUtils.registerOnSharedPreferenceChangeListener(this);
}

public void stop() {
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(this);
}

Track.Id startNewTrack() {
TrackPoint segmentStartTrackPoint = trackPointCreator.createSegmentStartManual();
// Create new track
Expand Down
Loading