Skip to content

Commit 0a83e6f

Browse files
committed
allow multiple actions for @ReceiverAction
1 parent c8b8725 commit 0a83e6f

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

‎AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/ReceiverAction.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@
7777
public @interface ReceiverAction {
7878

7979
/**
80-
* Define the action's name. If this field isn't set the annotated method
81-
* name will be used.
80+
* Define a set of actions this method should handle. If this field isn't
81+
* set the annotated method name will be used.
8282
*
83-
* @return the action's name
83+
* @return the actions
8484
*/
85-
String value() default "";
85+
String[] value() default {};
8686

8787
/**
8888
* Define a set of data schemes to filter the Intent. If this field isn't

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ReceiverActionHandler.java‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,41 +79,40 @@ public void process(Element element, EReceiverHolder holder) throws Exception {
7979

8080
ReceiverAction annotation = element.getAnnotation(ReceiverAction.class);
8181
String[] dataSchemes = annotation.dataSchemes();
82-
String extraKey = annotation.value();
83-
if (extraKey.isEmpty()) {
84-
extraKey = methodName;
82+
String[] actions = annotation.value();
83+
if (actions.length == 0) {
84+
actions = new String[] { methodName };
8585
}
8686

87-
JFieldVar actionKeyField = createStaticActionField(holder, extraKey, methodName);
88-
JFieldVar dataSchemesField = createStaticDataSchemesField(holder, dataSchemes, methodName);
87+
JFieldVar actionKeyField = createStaticField(holder, "actions", methodName, actions);
88+
JFieldVar dataSchemesField = createStaticField(holder, "dataSchemes", methodName, dataSchemes);
8989
addActionInOnReceive(holder, executableElement, methodName, actionKeyField, dataSchemesField);
9090
}
9191

92-
private JFieldVar createStaticActionField(EReceiverHolder holder, String extraKey, String methodName) {
93-
String staticFieldName = CaseHelper.camelCaseToUpperSnakeCase("action", methodName, null);
94-
return holder.getGeneratedClass().field(PUBLIC | STATIC | FINAL, classes().STRING, staticFieldName, lit(extraKey));
95-
}
92+
private JFieldVar createStaticField(EReceiverHolder holder, String prefix, String methodName, String[] values) {
93+
String staticFieldName = CaseHelper.camelCaseToUpperSnakeCase(prefix, methodName, null);
9694

97-
private JFieldVar createStaticDataSchemesField(EReceiverHolder holder, String[] dataSchemes, String methodName) {
98-
if (dataSchemes == null || dataSchemes.length == 0) {
95+
if (values == null || values.length == 0) {
9996
return null;
97+
} else if (values.length == 1) {
98+
return holder.getGeneratedClass().field(PUBLIC | STATIC | FINAL, classes().STRING, staticFieldName, lit(values[0]));
99+
100100
}
101-
JClass listOfStrings = classes().LIST.narrow(classes().STRING);
102-
String staticFieldName = CaseHelper.camelCaseToUpperSnakeCase("dataSchemes", methodName, null);
103101

104102
JInvocation asListInvoke = classes().ARRAYS.staticInvoke("asList");
105-
for (String scheme : dataSchemes) {
103+
for (String scheme : values) {
106104
asListInvoke.arg(scheme);
107105
}
108-
106+
JClass listOfStrings = classes().LIST.narrow(classes().STRING);
109107
return holder.getGeneratedClass().field(PUBLIC | STATIC | FINAL, listOfStrings, staticFieldName, asListInvoke);
110108
}
111109

112-
private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField, JFieldVar dataSchemesField) {
113-
// If action match, call the method
114-
JExpression filterCondition = actionKeyField.invoke("equals").arg(holder.getOnReceiveIntentAction());
110+
private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionsField, JFieldVar dataSchemesField) {
111+
String actionsInvoke = getInvocationName(actionsField);
112+
JExpression filterCondition = actionsField.invoke(actionsInvoke).arg(holder.getOnReceiveIntentAction());
115113
if (dataSchemesField != null) {
116-
filterCondition = filterCondition.cand(dataSchemesField.invoke("contains").arg(holder.getOnReceiveIntentDataScheme()));
114+
String dataSchemesInvoke = getInvocationName(dataSchemesField);
115+
filterCondition = filterCondition.cand(dataSchemesField.invoke(dataSchemesInvoke).arg(holder.getOnReceiveIntentDataScheme()));
117116
}
118117

119118
JBlock callActionBlock = holder.getOnReceiveBody()._if(filterCondition)._then();
@@ -142,6 +141,14 @@ private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement exec
142141
callActionBlock._return();
143142
}
144143

144+
private String getInvocationName(JFieldVar field) {
145+
JClass listOfStrings = classes().LIST.narrow(classes().STRING);
146+
if (field.type().fullName().equals(listOfStrings.fullName())) {
147+
return "contains";
148+
}
149+
return "equals";
150+
}
151+
145152
private static class ExtraHandler extends ExtraParameterHandler {
146153

147154
public ExtraHandler(ProcessingEnvironment processingEnvironment) {

0 commit comments

Comments
 (0)