If invalid value is passed to enum property, we will get a build error:
AAPT: error: '420' is incompatible with attribute trp_hourFormat (attr) enum [FORMAT_SYSTEM=0, FORMAT_12=1, FORMAT_24=2]
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<nl.joery.timerangepicker.TimeRangePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:trp_hourFormat="420" />
</LinearLayout>
But, nevertheless, if we pass a reference to integer with invalid value, we won't get any build error and code compiles successfully:
ints.xml
<resources>
<integer name="some_invalid_value">3</integer>
</resources>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<nl.joery.timerangepicker.TimeRangePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:trp_hourFormat="@integer/some_invalid_value" />
</LinearLayout>
Full code
In runtime, we will get exception with no message:
Caused by: java.lang.IllegalArgumentException
at nl.joery.timerangepicker.TimeRangePicker$HourFormat$Companion.fromId(TimeRangePicker.kt:1160)
at nl.joery.timerangepicker.TimeRangePicker.initAttributes(TimeRangePicker.kt:147)
at nl.joery.timerangepicker.TimeRangePicker.<init>(TimeRangePicker.kt:114)
at nl.joery.timerangepicker.TimeRangePicker.<init>(TimeRangePicker.kt:39)
at nl.joery.timerangepicker.TimeRangePicker.<init>(TimeRangePicker.kt)
Library tries to handle such cases but unsuccessfully:
https://github.com/Droppers/TimeRangePicker/blob/a5c73dbbcb87dcc6b5d1c1f1f5b57e5c330bccd9/timerangepicker/src/main/java/nl/joery/timerangepicker/TimeRangePicker.kt#L135-L140
https://github.com/Droppers/TimeRangePicker/blob/a5c73dbbcb87dcc6b5d1c1f1f5b57e5c330bccd9/timerangepicker/src/main/java/nl/joery/timerangepicker/TimeRangePicker.kt#L1081-L1086
Method fromId
never returns null, but throws exception with empty message on invalid value, but initAttributes
expects that fromId
returns null and assignes default value (HourFormat.FORMAT_12
) if returned enum value is null.
So, what's expected behavior? To leave default value, or to throw exception. If the last one, it's better to throw exception with some message like 'Invalid attribute value(trp_hourFormat=value)'