Skip to content

Commit 01cbb17

Browse files
committed
fix: preferences that are enums can be set either by integer or case-insensitive string
fixes #1874
1 parent 5925ce3 commit 01cbb17

File tree

34 files changed

+134
-123
lines changed

34 files changed

+134
-123
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- Try to not block the main thread when generating an Status Message, which causes an ANR
1515
- Messages that fail to send because the endpoint isn't ready now retry every 10 seconds, not every second
1616
- Import config screen displays JSON config LTR under RTL locales
17+
- setting / importing configuration options that are enums are now case-insensitive
18+
- Fix regression where setting the locatorPriority preference using a number wasn't working (#1874)
1719

1820
## Version 2.5.3
1921

project/app/src/androidTest/java/org/owntracks/android/mqtt/MQTTMessagePublishTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class MQTTMessagePublishTests :
196196
private fun setupTestActivity() {
197197
PreferenceManager.getDefaultSharedPreferences(app)
198198
.edit()
199-
.putInt(Preferences::monitoring.name, MonitoringMode.QUIET.value)
199+
.putInt(Preferences::monitoring.name, MonitoringMode.Quiet.value)
200200
.putString(Preferences::reverseGeocodeProvider.name, "None")
201201
.apply()
202202
setNotFirstStartPreferences()

project/app/src/androidTest/java/org/owntracks/android/ui/ClickOnPreference.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.adevinta.android.barista.interaction.BaristaClickInteractions.clickOn
99
import com.adevinta.android.barista.interaction.BaristaSleepInteractions.sleep
1010
import org.hamcrest.Matchers.allOf
1111

12-
private const val SLEEP_MILLIS = 100L
12+
private const val SLEEP_MILLIS = 10L
1313

1414
fun clickOnDrawerAndWait(text: Int) {
1515
Espresso.onView(

project/app/src/androidTest/java/org/owntracks/android/ui/ContactsActivityTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ContactsActivityTests :
4343
setNotFirstStartPreferences()
4444
getPreferences()
4545
.edit()
46-
.putInt(Preferences::monitoring.name, MonitoringMode.QUIET.value)
46+
.putInt(Preferences::monitoring.name, MonitoringMode.Quiet.value)
4747
.putString(Preferences::reverseGeocodeProvider.name, "None")
4848
.apply()
4949
launchActivity()

project/app/src/androidTest/java/org/owntracks/android/ui/PreferencesActivityTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class PreferencesActivityTests :
190190
val expected =
191191
baristaRule.activityTestRule.activity.resources.run {
192192
getStringArray(R.array.geocoders)[
193-
getStringArray(R.array.geocoderValues).indexOfFirst { it == defaultGeocoder.value }]
193+
getStringArray(R.array.geocoderValues).indexOfFirst { it == defaultGeocoder.name }]
194194
}
195195
assertContains(android.R.id.summary, expected)
196196
}

project/app/src/gms/java/org/owntracks/android/preferences/DefaultsProviderImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class DefaultsProviderImpl : DefaultsProvider {
99
override fun <T> getDefaultValue(preferences: Preferences, property: KProperty<*>): T {
1010
return when (property) {
1111
Preferences::mapLayerStyle -> MapLayerStyle.GoogleMapDefault
12-
Preferences::reverseGeocodeProvider -> ReverseGeocodeProvider.DEVICE
12+
Preferences::reverseGeocodeProvider -> ReverseGeocodeProvider.Device
1313
else -> super.getDefaultValue<T>(preferences, property)
1414
}
1515
as T

project/app/src/gms/java/org/owntracks/android/ui/map/MapLayerStyle.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ enum class MapLayerStyle {
3434
@JvmStatic
3535
@FromConfiguration
3636
fun getByValue(value: String): MapLayerStyle =
37-
entries.firstOrNull { it.name == value } ?: GoogleMapDefault
37+
entries.firstOrNull { it.name.equals(value, true) } ?: GoogleMapDefault
3838
}
3939
}
4040

project/app/src/main/java/org/owntracks/android/App.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ class App :
193193
@MainThread
194194
private fun setThemeFromPreferences() {
195195
when (preferences.theme) {
196-
AppTheme.AUTO -> AppCompatDelegate.setDefaultNightMode(Preferences.SYSTEM_NIGHT_AUTO_MODE)
197-
AppTheme.DARK -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
198-
AppTheme.LIGHT -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
196+
AppTheme.Auto -> AppCompatDelegate.setDefaultNightMode(Preferences.SYSTEM_NIGHT_AUTO_MODE)
197+
AppTheme.Dark -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
198+
AppTheme.Light -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
199199
}
200200
}
201201

project/app/src/main/java/org/owntracks/android/geocoding/GeocoderProvider.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ constructor(
5151
withContext(ioDispatcher) {
5252
geocoder =
5353
when (preferences.reverseGeocodeProvider) {
54-
ReverseGeocodeProvider.OPENCAGE ->
54+
ReverseGeocodeProvider.OpenCage ->
5555
OpenCageGeocoder(preferences.opencageApiKey, httpClient)
56-
ReverseGeocodeProvider.DEVICE -> DeviceGeocoder(context)
57-
ReverseGeocodeProvider.NONE -> GeocoderNone()
56+
ReverseGeocodeProvider.Device -> DeviceGeocoder(context)
57+
ReverseGeocodeProvider.None -> GeocoderNone()
5858
}
5959
}
6060
}

project/app/src/main/java/org/owntracks/android/location/LocatorPriority.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ package org.owntracks.android.location
22

33
import org.owntracks.android.preferences.types.FromConfiguration
44

5-
enum class LocatorPriority {
6-
HighAccuracy,
7-
BalancedPowerAccuracy,
8-
LowPower,
9-
NoPower;
5+
enum class LocatorPriority(private val value: Int) {
6+
HighAccuracy(3),
7+
BalancedPowerAccuracy(2),
8+
LowPower(1),
9+
NoPower(0);
1010

1111
companion object {
1212
@JvmStatic
1313
@FromConfiguration
14-
fun getByValue(value: String?): LocatorPriority? =
15-
LocatorPriority.entries.firstOrNull { it.name == value }
14+
fun getByValue(value: Int): LocatorPriority =
15+
LocatorPriority.entries.firstOrNull { it.value == value } ?: BalancedPowerAccuracy
16+
17+
@JvmStatic
18+
@FromConfiguration
19+
fun getByValue(value: String): LocatorPriority =
20+
value.toIntOrNull()?.run(::getByValue)
21+
?: entries.firstOrNull { it.name.equals(value, true) }
22+
?: BalancedPowerAccuracy
1623
}
1724
}

project/app/src/main/java/org/owntracks/android/preferences/DefaultsProvider.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface DefaultsProvider {
4141
Preferences::locatorInterval -> 60
4242
Preferences::locatorPriority -> null
4343
Preferences::mode -> ConnectionMode.MQTT
44-
Preferences::monitoring -> MonitoringMode.SIGNIFICANT
44+
Preferences::monitoring -> MonitoringMode.Significant
4545
Preferences::moveModeLocatorInterval -> 10
4646
Preferences::mqttProtocolLevel -> MqttProtocolLevel.MQTT_3_1
4747
Preferences::notificationEvents -> true
@@ -55,7 +55,7 @@ interface DefaultsProvider {
5555
Preferences::ping -> 15
5656
Preferences::port -> 8883
5757
Preferences::extendedData -> true
58-
Preferences::pubQos -> MqttQos.ONE
58+
Preferences::pubQos -> MqttQos.One
5959
Preferences::pubRetain -> true
6060
Preferences::pubTopicBase -> "owntracks/%u/%d"
6161
Preferences::publishLocationOnConnect -> false
@@ -64,9 +64,9 @@ interface DefaultsProvider {
6464
Preferences::setupCompleted -> false
6565
Preferences::showRegionsOnMap -> false
6666
Preferences::sub -> true
67-
Preferences::subQos -> MqttQos.TWO
67+
Preferences::subQos -> MqttQos.Two
6868
Preferences::subTopic -> DEFAULT_SUB_TOPIC
69-
Preferences::theme -> AppTheme.AUTO
69+
Preferences::theme -> AppTheme.Auto
7070
Preferences::tls -> true
7171
Preferences::tlsClientCrt -> ""
7272
Preferences::tid ->

project/app/src/main/java/org/owntracks/android/preferences/PreferenceDataStoreShim.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class PreferenceDataStoreShim @Inject constructor(private val preferences: Prefe
4444
override fun getString(key: String?, defValue: String?): String {
4545
val stringPreferenceValue =
4646
when (val preferenceValue = key?.run(preferences::getPreferenceByName) ?: defValue) {
47-
is ReverseGeocodeProvider -> preferenceValue.value
47+
is ReverseGeocodeProvider -> preferenceValue.name
4848
is StringMaxTwoAlphaNumericChars -> preferenceValue.toString()
4949
else -> preferenceValue
5050
}

project/app/src/main/java/org/owntracks/android/preferences/Preferences.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ constructor(
330330
return pubQos
331331
}
332332

333-
val pubQosWaypoints = MqttQos.ZERO
333+
val pubQosWaypoints = MqttQos.Zero
334334

335-
val pubQosStatus = MqttQos.ZERO
335+
val pubQosStatus = MqttQos.Zero
336336

337337
val pubRetainLocations: Boolean
338338
get() {

project/app/src/main/java/org/owntracks/android/preferences/PreferencesStore.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ abstract class PreferencesStore :
121121
typeOf<AppTheme>() -> AppTheme.getByValue(getInt(property.name, 0))
122122
typeOf<StringMaxTwoAlphaNumericChars>() ->
123123
StringMaxTwoAlphaNumericChars(getString(property.name, "") ?: "")
124-
typeOf<LocatorPriority?>() -> LocatorPriority.getByValue(getString(property.name, ""))
124+
typeOf<LocatorPriority?>() ->
125+
LocatorPriority.getByValue(getString(property.name, "") ?: "")
125126
else ->
126127
throw UnsupportedPreferenceTypeException(
127128
"Trying to get property ${property.name} has type ${property.returnType}")
@@ -212,7 +213,7 @@ abstract class PreferencesStore :
212213
is Int -> putInt(property.name, coercedValue)
213214
is Float -> putFloat(property.name, coercedValue)
214215
is Set<*> -> putStringSet(property.name, value as Set<String>)
215-
is ReverseGeocodeProvider -> putString(property.name, coercedValue.value)
216+
is ReverseGeocodeProvider -> putString(property.name, coercedValue.name)
216217
is MapLayerStyle -> putString(property.name, coercedValue.name)
217218
is ConnectionMode -> putInt(property.name, coercedValue.value)
218219
is MonitoringMode -> putInt(property.name, coercedValue.value)
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package org.owntracks.android.preferences.types
22

3-
import com.fasterxml.jackson.annotation.JsonValue
4-
5-
enum class AppTheme(@JsonValue val value: Int) {
6-
LIGHT(0),
7-
DARK(1),
8-
AUTO(2);
3+
enum class AppTheme(val value: Int) {
4+
Light(0),
5+
Dark(1),
6+
Auto(2);
97

108
companion object {
119
@JvmStatic
1210
@FromConfiguration
13-
fun getByValue(value: Int): AppTheme = entries.firstOrNull { it.value == value } ?: LIGHT
11+
fun getByValue(value: Int): AppTheme = entries.firstOrNull { it.value == value } ?: Auto
1412

1513
@JvmStatic
1614
@FromConfiguration
1715
fun getByValue(value: String): AppTheme =
18-
(value.toIntOrNull() ?: Int.MIN_VALUE).run(::getByValue)
16+
value.toIntOrNull()?.run(::getByValue)
17+
?: entries.firstOrNull { it.name.equals(value, true) }
18+
?: Auto
1919
}
2020
}

project/app/src/main/java/org/owntracks/android/preferences/types/ConnectionMode.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ enum class ConnectionMode(@JsonValue val value: Int) {
1414
@JvmStatic
1515
@FromConfiguration
1616
fun getByValue(value: String): ConnectionMode =
17-
(value.toIntOrNull() ?: Int.MIN_VALUE).run(::getByValue)
17+
value.toIntOrNull()?.run(::getByValue)
18+
?: entries.firstOrNull { it.name.equals(value, true) }
19+
?: MQTT
1820
}
1921
}

project/app/src/main/java/org/owntracks/android/preferences/types/MonitoringMode.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@ package org.owntracks.android.preferences.types
33
import com.fasterxml.jackson.annotation.JsonValue
44

55
enum class MonitoringMode(@JsonValue val value: Int) {
6-
QUIET(-1),
7-
MANUAL(0),
8-
SIGNIFICANT(1),
9-
MOVE(2);
6+
Quiet(-1),
7+
Manual(0),
8+
Significant(1),
9+
Move(2);
1010

1111
fun next(): MonitoringMode =
1212
when (this) {
13-
QUIET -> MANUAL
14-
MANUAL -> SIGNIFICANT
15-
SIGNIFICANT -> MOVE
16-
MOVE -> QUIET
13+
Quiet -> Manual
14+
Manual -> Significant
15+
Significant -> Move
16+
Move -> Quiet
1717
}
1818

1919
companion object {
2020
@JvmStatic
2121
@FromConfiguration
2222
fun getByValue(value: Int): MonitoringMode =
23-
entries.firstOrNull { it.value == value } ?: SIGNIFICANT
23+
entries.firstOrNull { it.value == value } ?: Significant
2424

2525
@JvmStatic
2626
@FromConfiguration
2727
fun getByValue(value: String): MonitoringMode =
28-
(value.toIntOrNull() ?: Int.MIN_VALUE).run(::getByValue)
28+
value.toIntOrNull()?.run(::getByValue)
29+
?: entries.firstOrNull { it.name.equals(value, true) }
30+
?: Significant
2931
}
3032
}

project/app/src/main/java/org/owntracks/android/preferences/types/MqttProtocolLevel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ enum class MqttProtocolLevel(@JsonValue val value: Int) {
1515
@JvmStatic
1616
@FromConfiguration
1717
fun getByValue(value: String): MqttProtocolLevel =
18-
(value.toIntOrNull() ?: Int.MIN_VALUE).run(::getByValue)
18+
value.toIntOrNull()?.run(::getByValue)
19+
?: entries.firstOrNull { it.name.equals(value, true) }
20+
?: MQTT_3_1
1921
}
2022
}

project/app/src/main/java/org/owntracks/android/preferences/types/MqttQos.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ package org.owntracks.android.preferences.types
33
import com.fasterxml.jackson.annotation.JsonValue
44

55
enum class MqttQos(@JsonValue val value: Int) {
6-
ZERO(0),
7-
ONE(1),
8-
TWO(2);
6+
Zero(0),
7+
One(1),
8+
Two(2);
99

1010
companion object {
1111
@JvmStatic
1212
@FromConfiguration
13-
fun getByValue(value: Int): MqttQos = entries.firstOrNull { it.value == value } ?: ONE
13+
fun getByValue(value: Int): MqttQos = entries.firstOrNull { it.value == value } ?: One
1414

1515
@JvmStatic
1616
@FromConfiguration
1717
fun getByValue(value: String): MqttQos =
18-
(value.toIntOrNull() ?: Int.MIN_VALUE).run(::getByValue)
18+
value.toIntOrNull()?.run(::getByValue)
19+
?: entries.firstOrNull { it.name.equals(value, true) }
20+
?: One
1921
}
2022
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package org.owntracks.android.preferences.types
22

3-
import com.fasterxml.jackson.annotation.JsonValue
4-
5-
enum class ReverseGeocodeProvider(@JsonValue val value: String) {
6-
NONE("None"),
7-
DEVICE("Device"),
8-
OPENCAGE("OpenCage");
3+
enum class ReverseGeocodeProvider {
4+
None,
5+
Device,
6+
OpenCage;
97

108
companion object {
119
@JvmStatic
1210
@FromConfiguration
1311
fun getByValue(value: String): ReverseGeocodeProvider =
14-
entries.firstOrNull { it.value == value } ?: NONE
12+
entries.firstOrNull { it.name.equals(value, true) } ?: None
1513
}
1614
}

project/app/src/main/java/org/owntracks/android/services/BackgroundService.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,20 +520,20 @@ class BackgroundService : LifecycleService(), Preferences.OnPreferenceChangeList
520520
var smallestDisplacement: Float? = null
521521
val priority: LocatorPriority
522522
when (monitoring) {
523-
MonitoringMode.QUIET,
524-
MonitoringMode.MANUAL -> {
523+
MonitoringMode.Quiet,
524+
MonitoringMode.Manual -> {
525525
interval = Duration.ofSeconds(preferences.locatorInterval.toLong())
526526
smallestDisplacement = preferences.locatorDisplacement.toFloat()
527527
priority = preferences.locatorPriority ?: LocatorPriority.LowPower
528528
}
529529

530-
MonitoringMode.SIGNIFICANT -> {
530+
MonitoringMode.Significant -> {
531531
interval = Duration.ofSeconds(preferences.locatorInterval.toLong())
532532
smallestDisplacement = preferences.locatorDisplacement.toFloat()
533533
priority = preferences.locatorPriority ?: LocatorPriority.BalancedPowerAccuracy
534534
}
535535

536-
MonitoringMode.MOVE -> {
536+
MonitoringMode.Move -> {
537537
interval = Duration.ofSeconds(preferences.moveModeLocatorInterval.toLong())
538538
priority = preferences.locatorPriority ?: LocatorPriority.HighAccuracy
539539
}

project/app/src/main/java/org/owntracks/android/services/LocationProcessor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ constructor(
9393
MessageTransition.TRIGGER_LOCATION)
9494
}
9595
}
96-
if (preferences.monitoring === MonitoringMode.QUIET &&
96+
if (preferences.monitoring === MonitoringMode.Quiet &&
9797
MessageLocation.ReportType.USER != trigger) {
9898
Timber.v("message suppressed by monitoring settings: quiet")
9999
return Result.failure(Exception("message suppressed by monitoring settings: quiet"))
100100
}
101-
if (preferences.monitoring === MonitoringMode.MANUAL &&
101+
if (preferences.monitoring === MonitoringMode.Manual &&
102102
MessageLocation.ReportType.USER != trigger &&
103103
MessageLocation.ReportType.CIRCULAR != trigger) {
104104
Timber.v("message suppressed by monitoring settings: manual")
@@ -189,7 +189,7 @@ constructor(
189189
waypointModel.lastTransition = transition
190190
waypointModel.lastTriggered = Instant.now()
191191
waypointsRepo.update(waypointModel, false)
192-
if (preferences.monitoring === MonitoringMode.QUIET) {
192+
if (preferences.monitoring === MonitoringMode.Quiet) {
193193
Timber.v("message suppressed by monitoring settings: ${preferences.monitoring}")
194194
} else {
195195
publishTransitionMessage(waypointModel, location, transition, trigger)

project/app/src/main/java/org/owntracks/android/services/OngoingNotification.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ class OngoingNotification(private val context: Context, private val initialMode:
126126
private fun getMonitoringLabel(monitoringMode: MonitoringMode) =
127127
context.run {
128128
when (monitoringMode) {
129-
MonitoringMode.QUIET -> getString(R.string.monitoring_quiet)
130-
MonitoringMode.MANUAL -> getString(R.string.monitoring_manual)
131-
MonitoringMode.SIGNIFICANT -> getString(R.string.monitoring_significant)
132-
MonitoringMode.MOVE -> getString(R.string.monitoring_move)
129+
MonitoringMode.Quiet -> getString(R.string.monitoring_quiet)
130+
MonitoringMode.Manual -> getString(R.string.monitoring_manual)
131+
MonitoringMode.Significant -> getString(R.string.monitoring_significant)
132+
MonitoringMode.Move -> getString(R.string.monitoring_move)
133133
}
134134
}
135135

0 commit comments

Comments
 (0)