From 989fe398490477f12aa1cc7aa24d5bc638d01b67 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 4 Sep 2022 11:31:42 +0200 Subject: [PATCH] Cleanup: BLE preferences. Part of #1352. --- .../settings/SensorsSettingsFragment.java | 19 ++----------------- ...othLeCyclingCadenceAndSpeedPreference.java | 9 +++++++++ .../BluetoothLeCyclingPowerPreference.java | 9 +++++++++ .../BluetoothLeHeartRatePreference.java | 9 +++++++++ ...othLeRunningSpeedAndCadencePreference.java | 9 +++++++++ .../BluetoothLeSensorPreference.java | 2 ++ 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java b/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java index af6cb580e..6650fa3e1 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java +++ b/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java @@ -9,11 +9,6 @@ import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import de.dennisguse.opentracks.R; -import de.dennisguse.opentracks.sensors.BluetoothUtils; -import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeCyclingCadenceAndSpeedPreference; -import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeCyclingPowerPreference; -import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeHeartRatePreference; -import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeRunningSpeedAndCadencePreference; import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeSensorPreference; public class SensorsSettingsFragment extends PreferenceFragmentCompat { @@ -34,18 +29,8 @@ public class SensorsSettingsFragment extends PreferenceFragmentCompat { @Override public void onDisplayPreferenceDialog(Preference preference) { - DialogFragment dialogFragment = null; - if (preference instanceof BluetoothLeHeartRatePreference) { - dialogFragment = BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(preference.getKey(), BluetoothUtils.HEART_RATE_SUPPORTING_DEVICES); - } else if (preference instanceof BluetoothLeCyclingCadenceAndSpeedPreference) { - dialogFragment = BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(preference.getKey(), BluetoothUtils.CYCLING_SPEED_CADENCE_SERVICE_UUID); - } else if (preference instanceof BluetoothLeCyclingPowerPreference) { - dialogFragment = BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(preference.getKey(), BluetoothUtils.CYCLING_POWER_UUID); - } else if (preference instanceof BluetoothLeRunningSpeedAndCadencePreference) { - dialogFragment = BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(preference.getKey(), BluetoothUtils.RUNNING_RUNNING_SPEED_CADENCE_UUID); - } - - if (dialogFragment != null) { + if (preference instanceof BluetoothLeSensorPreference) { + DialogFragment dialogFragment = ((BluetoothLeSensorPreference) preference).createInstance(); dialogFragment.setTargetFragment(this, 0); dialogFragment.show(getParentFragmentManager(), getClass().getSimpleName()); return; diff --git a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingCadenceAndSpeedPreference.java b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingCadenceAndSpeedPreference.java index aba91ba79..6a6369ff3 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingCadenceAndSpeedPreference.java +++ b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingCadenceAndSpeedPreference.java @@ -3,6 +3,10 @@ package de.dennisguse.opentracks.settings.bluetooth; import android.content.Context; import android.util.AttributeSet; +import androidx.preference.PreferenceDialogFragmentCompat; + +import de.dennisguse.opentracks.sensors.BluetoothUtils; + public class BluetoothLeCyclingCadenceAndSpeedPreference extends BluetoothLeSensorPreference { public BluetoothLeCyclingCadenceAndSpeedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { @@ -20,4 +24,9 @@ public class BluetoothLeCyclingCadenceAndSpeedPreference extends BluetoothLeSens public BluetoothLeCyclingCadenceAndSpeedPreference(Context context) { super(context); } + + @Override + public PreferenceDialogFragmentCompat createInstance() { + return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothUtils.CYCLING_SPEED_CADENCE_SERVICE_UUID); + } } diff --git a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingPowerPreference.java b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingPowerPreference.java index 486136bea..eafd65608 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingPowerPreference.java +++ b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeCyclingPowerPreference.java @@ -3,6 +3,10 @@ package de.dennisguse.opentracks.settings.bluetooth; import android.content.Context; import android.util.AttributeSet; +import androidx.preference.PreferenceDialogFragmentCompat; + +import de.dennisguse.opentracks.sensors.BluetoothUtils; + public class BluetoothLeCyclingPowerPreference extends BluetoothLeSensorPreference { public BluetoothLeCyclingPowerPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { @@ -20,4 +24,9 @@ public class BluetoothLeCyclingPowerPreference extends BluetoothLeSensorPreferen public BluetoothLeCyclingPowerPreference(Context context) { super(context); } + + @Override + public PreferenceDialogFragmentCompat createInstance() { + return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothUtils.CYCLING_POWER_UUID); + } } \ No newline at end of file diff --git a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeHeartRatePreference.java b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeHeartRatePreference.java index 8021228a9..db6df1979 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeHeartRatePreference.java +++ b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeHeartRatePreference.java @@ -3,6 +3,10 @@ package de.dennisguse.opentracks.settings.bluetooth; import android.content.Context; import android.util.AttributeSet; +import androidx.preference.PreferenceDialogFragmentCompat; + +import de.dennisguse.opentracks.sensors.BluetoothUtils; + public class BluetoothLeHeartRatePreference extends BluetoothLeSensorPreference { public BluetoothLeHeartRatePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { @@ -20,4 +24,9 @@ public class BluetoothLeHeartRatePreference extends BluetoothLeSensorPreference public BluetoothLeHeartRatePreference(Context context) { super(context); } + + @Override + public PreferenceDialogFragmentCompat createInstance() { + return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothUtils.HEART_RATE_SUPPORTING_DEVICES); + } } \ No newline at end of file diff --git a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeRunningSpeedAndCadencePreference.java b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeRunningSpeedAndCadencePreference.java index 055fcb29f..a7653a400 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeRunningSpeedAndCadencePreference.java +++ b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeRunningSpeedAndCadencePreference.java @@ -3,6 +3,10 @@ package de.dennisguse.opentracks.settings.bluetooth; import android.content.Context; import android.util.AttributeSet; +import androidx.preference.PreferenceDialogFragmentCompat; + +import de.dennisguse.opentracks.sensors.BluetoothUtils; + public class BluetoothLeRunningSpeedAndCadencePreference extends BluetoothLeSensorPreference { public BluetoothLeRunningSpeedAndCadencePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { @@ -20,4 +24,9 @@ public class BluetoothLeRunningSpeedAndCadencePreference extends BluetoothLeSens public BluetoothLeRunningSpeedAndCadencePreference(Context context) { super(context); } + + @Override + public PreferenceDialogFragmentCompat createInstance() { + return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothUtils.RUNNING_RUNNING_SPEED_CADENCE_UUID); + } } diff --git a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeSensorPreference.java b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeSensorPreference.java index c3d759eb1..50d75ca9a 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeSensorPreference.java +++ b/src/main/java/de/dennisguse/opentracks/settings/bluetooth/BluetoothLeSensorPreference.java @@ -92,6 +92,8 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference { return getValue(); } + public abstract PreferenceDialogFragmentCompat createInstance(); + public static class BluetoothLeSensorPreferenceDialog extends PreferenceDialogFragmentCompat { private AnimatedVectorDrawableCompat bluetoothIcon;