forked from upstream-mirrors/OpenTracks
Cleanup: merged BluetoothRemoteSensorManager into SensorManager.
Preparation for refactoring.
This commit is contained in:
@@ -1,126 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010 Google Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
||||||
* use this file except in compliance with the License. You may obtain a copy of
|
|
||||||
* the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
* License for the specific language governing permissions and limitations under
|
|
||||||
* the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.dennisguse.opentracks.sensors;
|
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.os.Handler;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import de.dennisguse.opentracks.R;
|
|
||||||
import de.dennisguse.opentracks.sensors.driver.DriverObserver0;
|
|
||||||
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bluetooth LE sensor manager: manages connections to Bluetooth LE sensors.
|
|
||||||
* <p>
|
|
||||||
* Note: should only be instantiated once.
|
|
||||||
* TODO: listen for Bluetooth enabled/disabled events.
|
|
||||||
* @author Sandor Dornbush
|
|
||||||
*/
|
|
||||||
//TODO Merge into SensorManager
|
|
||||||
public class BluetoothRemoteSensorManager implements SharedPreferences.OnSharedPreferenceChangeListener {
|
|
||||||
|
|
||||||
private static final String TAG = BluetoothRemoteSensorManager.class.getSimpleName();
|
|
||||||
|
|
||||||
private final Context context;
|
|
||||||
private final Handler handler;
|
|
||||||
private boolean started = false;
|
|
||||||
|
|
||||||
private final BluetoothConnectionManager heartRate;
|
|
||||||
private final BluetoothConnectionManager temperature;
|
|
||||||
private final BluetoothConnectionManager cyclingCadence;
|
|
||||||
private final BluetoothConnectionManager cyclingSpeed;
|
|
||||||
private final BluetoothConnectionManager cyclingPower;
|
|
||||||
private final BluetoothConnectionManager runningSpeedAndCadence;
|
|
||||||
|
|
||||||
public BluetoothRemoteSensorManager(@NonNull Context context, @NonNull Handler handler, @Nullable DriverObserver0 observer) {
|
|
||||||
this.context = context;
|
|
||||||
this.handler = handler;
|
|
||||||
|
|
||||||
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(context);
|
|
||||||
this.heartRate = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerHeartRate());
|
|
||||||
this.temperature = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerTemperature());
|
|
||||||
this.cyclingCadence = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerCyclingCadence());
|
|
||||||
this.cyclingSpeed = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerCyclingDistanceSpeed());
|
|
||||||
this.cyclingPower = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerCyclingPower());
|
|
||||||
this.runningSpeedAndCadence = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerRunningSpeedAndCadence());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void start() {
|
|
||||||
started = true;
|
|
||||||
|
|
||||||
// Triggers connection startup
|
|
||||||
onSharedPreferenceChanged(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void stop() {
|
|
||||||
heartRate.disconnect();
|
|
||||||
temperature.disconnect();
|
|
||||||
cyclingCadence.disconnect();
|
|
||||||
cyclingSpeed.disconnect();
|
|
||||||
cyclingPower.disconnect();
|
|
||||||
runningSpeedAndCadence.disconnect();
|
|
||||||
|
|
||||||
started = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private synchronized void connect(BluetoothConnectionManager connectionManager, String address) {
|
|
||||||
connectionManager.connect(context, handler, address);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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_temperature_key, key)) {
|
|
||||||
String address = PreferencesUtils.getBluetoothTemperatureSensorAddress();
|
|
||||||
connect(temperature, 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.dennisguse.opentracks.sensors;
|
package de.dennisguse.opentracks.sensors;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
@@ -12,6 +13,7 @@ import androidx.annotation.VisibleForTesting;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.R;
|
||||||
import de.dennisguse.opentracks.data.models.Cadence;
|
import de.dennisguse.opentracks.data.models.Cadence;
|
||||||
import de.dennisguse.opentracks.data.models.Distance;
|
import de.dennisguse.opentracks.data.models.Distance;
|
||||||
import de.dennisguse.opentracks.data.models.HeartRate;
|
import de.dennisguse.opentracks.data.models.HeartRate;
|
||||||
@@ -40,11 +42,20 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
|
|
||||||
private static final String TAG = SensorManager.class.getSimpleName();
|
private static final String TAG = SensorManager.class.getSimpleName();
|
||||||
|
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
private Handler handler;
|
||||||
|
|
||||||
private PowerManager.WakeLock wakeLock;
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
|
||||||
private final TrackPointCreator observer;
|
private final TrackPointCreator observer;
|
||||||
|
|
||||||
private BluetoothRemoteSensorManager bluetoothSensorManager;
|
private BluetoothConnectionManager heartRateManager;
|
||||||
|
private BluetoothConnectionManager temperatureManager;
|
||||||
|
private BluetoothConnectionManager cyclingCadenceManager;
|
||||||
|
private BluetoothConnectionManager cyclingSpeedManager;
|
||||||
|
private BluetoothConnectionManager cyclingPowerManager;
|
||||||
|
private BluetoothConnectionManager runningSpeedAndCadenceManager;
|
||||||
|
|
||||||
private GainManager altitudeSumManager;
|
private GainManager altitudeSumManager;
|
||||||
|
|
||||||
@@ -60,23 +71,45 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
}
|
}
|
||||||
|
|
||||||
wakeLock = SystemUtils.acquireWakeLock(context, wakeLock);
|
wakeLock = SystemUtils.acquireWakeLock(context, wakeLock);
|
||||||
|
this.context = context;
|
||||||
|
this.handler = handler;
|
||||||
|
|
||||||
gpsManager = new GpsManager(observer, this);
|
gpsManager = new GpsManager(observer, this);
|
||||||
altitudeSumManager = new GainManager(this);
|
altitudeSumManager = new GainManager(this);
|
||||||
bluetoothSensorManager = new BluetoothRemoteSensorManager(context, handler, this);
|
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(context);
|
||||||
|
this.heartRateManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerHeartRate());
|
||||||
|
this.temperatureManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerTemperature());
|
||||||
|
this.cyclingCadenceManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerCyclingCadence());
|
||||||
|
this.cyclingSpeedManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerCyclingDistanceSpeed());
|
||||||
|
this.cyclingPowerManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerCyclingPower());
|
||||||
|
this.runningSpeedAndCadenceManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerRunningSpeedAndCadence());
|
||||||
|
|
||||||
onSharedPreferenceChanged(null, null);
|
onSharedPreferenceChanged(null, null);
|
||||||
|
|
||||||
gpsManager.start(context, handler);
|
gpsManager.start(context, handler);
|
||||||
altitudeSumManager.start(context, handler);
|
altitudeSumManager.start(context, handler);
|
||||||
bluetoothSensorManager.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
wakeLock = SystemUtils.releaseWakeLock(wakeLock);
|
wakeLock = SystemUtils.releaseWakeLock(wakeLock);
|
||||||
|
|
||||||
bluetoothSensorManager.stop();
|
heartRateManager.disconnect();
|
||||||
bluetoothSensorManager = null;
|
heartRateManager = null;
|
||||||
|
|
||||||
|
temperatureManager.disconnect();
|
||||||
|
temperatureManager = null;
|
||||||
|
|
||||||
|
cyclingCadenceManager.disconnect();
|
||||||
|
cyclingCadenceManager = null;
|
||||||
|
|
||||||
|
cyclingSpeedManager.disconnect();
|
||||||
|
cyclingSpeedManager = null;
|
||||||
|
|
||||||
|
cyclingPowerManager.disconnect();
|
||||||
|
cyclingPowerManager = null;
|
||||||
|
|
||||||
|
runningSpeedAndCadenceManager.disconnect();
|
||||||
|
runningSpeedAndCadenceManager = null;
|
||||||
|
|
||||||
altitudeSumManager.stop();
|
altitudeSumManager.stop();
|
||||||
altitudeSumManager = null;
|
altitudeSumManager = null;
|
||||||
@@ -84,11 +117,14 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
gpsManager.stop();
|
gpsManager.stop();
|
||||||
gpsManager = null;
|
gpsManager = null;
|
||||||
|
|
||||||
|
context = null;
|
||||||
|
handler = null;
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
if (bluetoothSensorManager == null || altitudeSumManager == null) {
|
if (gpsManager == null || altitudeSumManager == null) {
|
||||||
Log.d(TAG, "No recording running and no reset necessary.");
|
Log.d(TAG, "No recording running and no reset necessary.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -121,11 +157,47 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
this.altitudeSumManager = altitudeSumManager;
|
this.altitudeSumManager = altitudeSumManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized void connect(BluetoothConnectionManager connectionManager, String address) {
|
||||||
|
connectionManager.connect(context, handler, address);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) {
|
||||||
if (gpsManager != null) {
|
if (gpsManager != null) {
|
||||||
gpsManager.onSharedPreferenceChanged(sharedPreferences, key);
|
gpsManager.onSharedPreferenceChanged(sharedPreferences, key);
|
||||||
bluetoothSensorManager.onSharedPreferenceChanged(sharedPreferences, key);
|
|
||||||
|
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_heart_rate_key, key)) {
|
||||||
|
String address = PreferencesUtils.getBluetoothHeartRateSensorAddress();
|
||||||
|
connect(heartRateManager, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_temperature_key, key)) {
|
||||||
|
String address = PreferencesUtils.getBluetoothTemperatureSensorAddress();
|
||||||
|
connect(temperatureManager, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_cadence_key, key)) {
|
||||||
|
String address = PreferencesUtils.getBluetoothCyclingCadenceSensorAddress();
|
||||||
|
connect(cyclingCadenceManager, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_speed_key, key)) {
|
||||||
|
String address = PreferencesUtils.getBluetoothCyclingSpeedSensorAddress();
|
||||||
|
|
||||||
|
connect(cyclingSpeedManager, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_power_key, key)) {
|
||||||
|
String address = PreferencesUtils.getBluetoothCyclingPowerSensorAddress();
|
||||||
|
|
||||||
|
connect(cyclingPowerManager, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_running_speed_and_cadence_key, key)) {
|
||||||
|
String address = PreferencesUtils.getBluetoothRunningSpeedAndCadenceAddress();
|
||||||
|
|
||||||
|
connect(runningSpeedAndCadenceManager, address);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user