Issue
I wish to add the following attribute to a custom view I'm building in Android:
<attr name="sourceType" format="enum">
<enum name="generic" value="???" />
<enum name="dash" value="???" />
<enum name="smooth_streaming" value="???" />
<enum name="hls" value="???" />
</attr>
Internally in my code I would like to use an enum to represent the various source types:
public enum SourceType {
Generic, DASH, SmoothStreaming, HLS;
}
However in my custom view I'm not sure how to convert the attribute value to an enum:
public BFPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.BFPlayer);
// Clearly wrong:
// 1. SourceType.Generic cannot be cast to int
// 2. int cannot be cast to SourceType
SourceType sourceType = attributes.getInt(R.styleable.BFPlayer_sourceType, SourceType.Generic);
}
What I've tried
I've considered doing something like the following:
attrs.xml
<attr name="sourceType" format="enum">
<enum name="generic" value="1" />
<enum name="dash" value="2" />
<enum name="smooth_streaming" value="3" />
<enum name="hls" value="4" />
</attr>
SourceType.java
public enum SourceType {
Generic (1), DASH (2), SmoothStreaming (3), HLS (4);
private int value;
private SourceType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static SourceType fromInt(int value) {
switch (value) {
case 1: return Generic;
case 2: return DASH;
case 3: return SmoothStremaing;
case 4: return HLS;
default: throw new Error("Invalid SourceType");
}
}
}
BFPlayer.java
public BFPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.BFPlayer);
// Clearly wrong:
// 1. SourceType.Generic cannot be cast to int
// 2. int cannot be cast to SourceType
SourceType sourceType = SourceType.fromInt(
attributes.getInt(R.styleable.BFPlayer_sourceType, SourceType.Generic.getValue())
);
}
However this feels like the wrong solution:
- It necessitates the use of
.fromtInt
and.getValue
to instantiate a new SourceType - It requires updating the switch statement if I add new values
- It arbitrarily assigns integer values to each option
Is there a better solution?