Skip to content

Commit bbed33f

Browse files
committed
allow injection of the Context and Intent of onReceive Call
1 parent 9af622f commit bbed33f

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
* You MAY use the {@link ReceiverAction.Extra} annotation on parameters to
3737
* define a different extra name.
3838
* </p>
39+
* <p>
40+
* Parameters of type {@link android.content.Intent} or
41+
* {@link android.content.Context} will get the corresponding parameter of
42+
* {@code void onReceive(Context context, Intent intent)} assigned.
43+
* </p>
3944
* <blockquote>
4045
*
4146
* Example :

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement exec
106106
// Extras params
107107
for (VariableElement param : methodParameters) {
108108
String paramName = param.getSimpleName().toString();
109+
JClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType(), holder);
110+
111+
if (extraParamClass.equals(classes().CONTEXT)) {
112+
callActionInvocation.arg(holder.getOnReceiveContext());
113+
continue;
114+
} else if (extraParamClass.equals(classes().INTENT)) {
115+
callActionInvocation.arg(holder.getOnReceiveIntent());
116+
continue;
117+
}
109118

110119
Extra annotation = param.getAnnotation(ReceiverAction.Extra.class);
111120
if (annotation != null && !annotation.value().isEmpty()) {
@@ -114,7 +123,6 @@ private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement exec
114123

115124
String extraParamName = paramName.replaceAll("\\.", "_") + "_Extra";
116125
JFieldVar paramVar = getStaticExtraField(holder, paramName);
117-
JClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType(), holder);
118126
BundleHelper bundleHelper = new BundleHelper(annotationHelper, param);
119127

120128
JExpression getExtraExpression = JExpr.invoke(extras, bundleHelper.getMethodNameToRestore()).arg(paramVar);

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class EReceiverHolder extends EComponentHolder {
3535
private JBlock onReceiveBody;
3636
private JVar onReceiveIntentAction;
3737
private JVar onReceiveIntent;
38+
private JVar onReceiveContext;
3839
private JMethod onReceiveMethod;
3940

4041
public EReceiverHolder(ProcessHolder processHolder, TypeElement annotatedElement) throws Exception {
@@ -57,13 +58,13 @@ protected void setInit() {
5758

5859
private void createOnReceive() {
5960
onReceiveMethod = generatedClass.method(PUBLIC, codeModel().VOID, "onReceive");
60-
JVar contextParam = onReceiveMethod.param(classes().CONTEXT, "context");
61+
onReceiveContext = onReceiveMethod.param(classes().CONTEXT, "context");
6162
onReceiveIntent = onReceiveMethod.param(classes().INTENT, "intent");
6263
onReceiveMethod.annotate(Override.class);
6364
onReceiveBody = onReceiveMethod.body();
64-
onReceiveBody.assign(getContextField(), contextParam);
65+
onReceiveBody.assign(getContextField(), onReceiveContext);
6566
onReceiveBody.invoke(getInit());
66-
onReceiveBody.invoke(JExpr._super(), onReceiveMethod).arg(contextParam).arg(onReceiveIntent);
67+
onReceiveBody.invoke(JExpr._super(), onReceiveMethod).arg(onReceiveContext).arg(onReceiveIntent);
6768

6869
JInvocation getActionInvocation = JExpr.invoke(onReceiveIntent, "getAction");
6970
onReceiveIntentAction = onReceiveBody.decl(classes().STRING, "action", getActionInvocation);
@@ -90,6 +91,13 @@ public JVar getOnReceiveIntent() {
9091
return onReceiveIntent;
9192
}
9293

94+
public JVar getOnReceiveContext() {
95+
if (onReceiveContext == null) {
96+
createOnReceive();
97+
}
98+
return onReceiveContext;
99+
}
100+
93101
public JVar getOnReceiveIntentAction() {
94102
if (onReceiveIntentAction == null) {
95103
createOnReceive();

0 commit comments

Comments
 (0)