Skip to content

Commit 5a6db4d

Browse files
committed
Merge pull request androidannotations#1234 from dodgex/1232_mutliple_actions_for_receiveraction
1232 mutliple actions for receiveraction
2 parents 384e527 + 5539bf9 commit 5a6db4d

5 files changed

Lines changed: 91 additions & 55 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) {

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EReceiverHolder.java‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424

2525
import com.sun.codemodel.JBlock;
2626
import com.sun.codemodel.JExpr;
27-
import com.sun.codemodel.JFieldVar;
2827
import com.sun.codemodel.JInvocation;
2928
import com.sun.codemodel.JMethod;
3029
import com.sun.codemodel.JVar;
3130

3231
public class EReceiverHolder extends EComponentHolder {
3332

34-
private JFieldVar contextField;
3533
private JBlock onReceiveBody;
3634
private JVar onReceiveIntentAction;
3735
private JVar onReceiveIntentDataScheme;
@@ -45,13 +43,15 @@ public EReceiverHolder(ProcessHolder processHolder, TypeElement annotatedElement
4543

4644
@Override
4745
protected void setContextRef() {
48-
contextField = generatedClass.field(PRIVATE, classes().CONTEXT, "context_");
49-
contextRef = contextField;
46+
if (init == null) {
47+
setInit();
48+
}
5049
}
5150

5251
@Override
5352
protected void setInit() {
5453
init = generatedClass.method(PRIVATE, codeModel().VOID, "init_");
54+
contextRef = init.param(classes().CONTEXT, "context");
5555
if (onReceiveMethod == null) {
5656
createOnReceive();
5757
}
@@ -63,14 +63,18 @@ private void createOnReceive() {
6363
onReceiveIntent = onReceiveMethod.param(classes().INTENT, "intent");
6464
onReceiveMethod.annotate(Override.class);
6565
onReceiveBody = onReceiveMethod.body();
66-
onReceiveBody.assign(getContextField(), onReceiveContext);
67-
onReceiveBody.invoke(getInit());
66+
onReceiveBody.invoke(getInit()).arg(onReceiveContext);
6867
onReceiveBody.invoke(JExpr._super(), onReceiveMethod).arg(onReceiveContext).arg(onReceiveIntent);
68+
}
6969

70-
JInvocation getActionInvocation = JExpr.invoke(onReceiveIntent, "getAction");
71-
JInvocation getDataSchemeInvocation = JExpr.invoke(onReceiveIntent, "getScheme");
72-
onReceiveIntentAction = onReceiveBody.decl(classes().STRING, "action", getActionInvocation);
73-
onReceiveIntentDataScheme = onReceiveBody.decl(classes().STRING, "dataScheme", getDataSchemeInvocation);
70+
private void setOnReceiveIntentAction() {
71+
JInvocation getActionInvocation = JExpr.invoke(getOnReceiveIntent(), "getAction");
72+
onReceiveIntentAction = getOnReceiveBody().decl(classes().STRING, "action", getActionInvocation);
73+
}
74+
75+
private void setOnReceiveIntentDataScheme() {
76+
JInvocation getDataSchemeInvocation = JExpr.invoke(getOnReceiveIntent(), "getScheme");
77+
onReceiveIntentDataScheme = getOnReceiveBody().decl(classes().STRING, "dataScheme", getDataSchemeInvocation);
7478
}
7579

7680
public JMethod getOnReceiveMethod() {
@@ -103,22 +107,15 @@ public JVar getOnReceiveContext() {
103107

104108
public JVar getOnReceiveIntentAction() {
105109
if (onReceiveIntentAction == null) {
106-
createOnReceive();
110+
setOnReceiveIntentAction();
107111
}
108112
return onReceiveIntentAction;
109113
}
110114

111115
public JVar getOnReceiveIntentDataScheme() {
112116
if (onReceiveIntentDataScheme == null) {
113-
createOnReceive();
117+
setOnReceiveIntentDataScheme();
114118
}
115119
return onReceiveIntentDataScheme;
116120
}
117-
118-
public JFieldVar getContextField() {
119-
if (contextField == null) {
120-
setContextRef();
121-
}
122-
return contextField;
123-
}
124121
}

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/ereceiver/ReceiverWithActions.java‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
@EReceiver
2626
public class ReceiverWithActions extends BroadcastReceiver {
2727

28+
public static final String ACTION_SIMPLE_TEST = "ACTION_SIMPLE_TEST";
29+
public static final String ACTION_SCHEME_TEST = "ACTION_SCHEME_TEST";
30+
public static final String ACTION_PARAMETER_TEST = "ACTION_PARAMETER_TEST";
31+
public static final String ACTION_MULTIPLE_TEST_1 = "ACTION_MULTIPLE_TEST_1";
32+
public static final String ACTION_MULTIPLE_TEST_2 = "ACTION_MULTIPLE_TEST_2";
33+
public static final String ACTION_EXTRA_PARAMETER_TEST = "ACTION_EXTRA_PARAMETER_TEST";
34+
public static final String EXTRA_ARG_NAME1 = "thisExtraHasAnotherName";
35+
public static final String EXTRA_ARG_NAME2 = "thisIsMyParameter";
36+
public static final String DATA_SCHEME = "http";
37+
2838
public boolean simpleActionReceived = false;
2939
public boolean actionWithSchemeReceived = false;
3040

@@ -34,29 +44,36 @@ public class ReceiverWithActions extends BroadcastReceiver {
3444
public boolean extraParameterActionReceived = false;
3545
public String extraParameterActionValue = null;
3646

47+
public int multipleActionCall = 0;
48+
3749
@Override
3850
public void onReceive(Context context, Intent intent) {
3951
}
4052

41-
@ReceiverAction("ACTION_SIMPLE_TEST")
53+
@ReceiverAction(ACTION_SIMPLE_TEST)
4254
public void onSimpleAction() {
4355
simpleActionReceived = true;
4456
}
4557

46-
@ReceiverAction(value = "ACTION_SCHEME_TEST", dataSchemes = "http")
58+
@ReceiverAction(value = ACTION_SCHEME_TEST, dataSchemes = DATA_SCHEME)
4759
public void onActionWithReceiver() {
4860
actionWithSchemeReceived = true;
4961
}
5062

51-
@ReceiverAction("ACTION_PARAMETER_TEST")
63+
@ReceiverAction(ACTION_PARAMETER_TEST)
5264
public void onParameterAction(@ReceiverAction.Extra String thisIsMyParameter) {
5365
parameterActionReceived = true;
5466
parameterActionValue = thisIsMyParameter;
5567
}
5668

57-
@ReceiverAction("ACTION_EXTRA_PARAMETER_TEST")
58-
public void onExtraParameterAction(@ReceiverAction.Extra("thisExtraHasAnotherName") String thisIsAParameter) {
69+
@ReceiverAction(ACTION_EXTRA_PARAMETER_TEST)
70+
public void onExtraParameterAction(@ReceiverAction.Extra(EXTRA_ARG_NAME1) String thisIsAParameter) {
5971
extraParameterActionReceived = true;
6072
extraParameterActionValue = thisIsAParameter;
6173
}
74+
75+
@ReceiverAction({ ACTION_MULTIPLE_TEST_1, ACTION_MULTIPLE_TEST_2 })
76+
public void onMultipleActions() {
77+
multipleActionCall++;
78+
}
6279
}

‎AndroidAnnotations/functional-test-1-5/src/test/java/org/androidannotations/test15/ereceiver/ReceiverWithActionsTest.java‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
@RunWith(RobolectricTestRunner.class)
3131
public class ReceiverWithActionsTest {
32+
private static final String EXTRA_PARAMETER_VALUE = "string value";
33+
3234
private ReceiverWithActions receiver;
3335

3436
@Before
@@ -38,36 +40,49 @@ public void setup() {
3840

3941
@Test
4042
public void onSimpleActionTest() {
41-
receiver.onReceive(Robolectric.application, new Intent("ACTION_SIMPLE_TEST"));
43+
receiver.onReceive(Robolectric.application, new Intent(ReceiverWithActions.ACTION_SIMPLE_TEST));
4244

4345
assertTrue(receiver.simpleActionReceived);
4446
}
4547

4648
@Test
4749
public void onActionWithReceiverTest() {
48-
Intent intent = new Intent("ACTION_SCHEME_TEST", Uri.parse("http://androidannotations.org"));
50+
Intent intent = new Intent(ReceiverWithActions.ACTION_SCHEME_TEST, Uri.parse(ReceiverWithActions.DATA_SCHEME + "://androidannotations.org"));
4951
receiver.onReceive(Robolectric.application, intent);
5052

5153
assertTrue(receiver.actionWithSchemeReceived);
5254
}
5355

5456
@Test
5557
public void onParameterActionTest() {
56-
Intent intent = new Intent("ACTION_PARAMETER_TEST");
57-
intent.putExtra("thisIsMyParameter", "string value");
58+
Intent intent = new Intent(ReceiverWithActions.ACTION_PARAMETER_TEST);
59+
intent.putExtra(ReceiverWithActions.EXTRA_ARG_NAME2, EXTRA_PARAMETER_VALUE);
5860
receiver.onReceive(Robolectric.application, intent);
5961

6062
assertTrue(receiver.parameterActionReceived);
61-
assertEquals("string value", receiver.parameterActionValue);
63+
assertEquals(EXTRA_PARAMETER_VALUE, receiver.parameterActionValue);
6264
}
6365

6466
@Test
6567
public void onExtraParameterActionTest() {
66-
Intent intent = new Intent("ACTION_EXTRA_PARAMETER_TEST");
67-
intent.putExtra("thisExtraHasAnotherName", "string value");
68+
Intent intent = new Intent(ReceiverWithActions.ACTION_EXTRA_PARAMETER_TEST);
69+
intent.putExtra(ReceiverWithActions.EXTRA_ARG_NAME1, EXTRA_PARAMETER_VALUE);
6870
receiver.onReceive(Robolectric.application, intent);
6971

7072
assertTrue(receiver.extraParameterActionReceived);
71-
assertEquals("string value", receiver.extraParameterActionValue);
73+
assertEquals(EXTRA_PARAMETER_VALUE, receiver.extraParameterActionValue);
74+
}
75+
76+
@Test
77+
public void onMultipleActionsTest() {
78+
assertEquals(0, receiver.multipleActionCall);
79+
80+
Intent intent = new Intent(ReceiverWithActions.ACTION_MULTIPLE_TEST_1);
81+
receiver.onReceive(Robolectric.application, intent);
82+
assertEquals(1, receiver.multipleActionCall);
83+
84+
intent = new Intent(ReceiverWithActions.ACTION_MULTIPLE_TEST_2);
85+
receiver.onReceive(Robolectric.application, intent);
86+
assertEquals(2, receiver.multipleActionCall);
7287
}
7388
}

0 commit comments

Comments
 (0)