Skip to content

Commit 7cf2dd0

Browse files
committed
handle dataSchemes for @ReceiverAction
1 parent cdf8c94 commit 7cf2dd0

3 files changed

Lines changed: 61 additions & 27 deletions

File tree

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

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,34 @@
1515
*/
1616
package org.androidannotations.handler;
1717

18-
import com.sun.codemodel.JBlock;
19-
import com.sun.codemodel.JClass;
20-
import com.sun.codemodel.JExpression;
21-
import com.sun.codemodel.JFieldVar;
22-
import com.sun.codemodel.JInvocation;
23-
import com.sun.codemodel.JOp;
24-
import com.sun.codemodel.JVar;
18+
import static com.sun.codemodel.JExpr._new;
19+
import static com.sun.codemodel.JExpr._null;
20+
import static com.sun.codemodel.JExpr.lit;
21+
import static com.sun.codemodel.JMod.FINAL;
22+
import static com.sun.codemodel.JMod.PUBLIC;
23+
import static com.sun.codemodel.JMod.STATIC;
24+
25+
import java.util.List;
26+
27+
import javax.annotation.processing.ProcessingEnvironment;
28+
import javax.lang.model.element.Element;
29+
import javax.lang.model.element.ExecutableElement;
30+
import javax.lang.model.element.VariableElement;
31+
2532
import org.androidannotations.annotations.ReceiverAction;
2633
import org.androidannotations.helper.APTCodeModelHelper;
2734
import org.androidannotations.helper.CaseHelper;
2835
import org.androidannotations.holder.EReceiverHolder;
2936
import org.androidannotations.model.AnnotationElements;
3037
import org.androidannotations.process.IsValid;
3138

32-
import javax.annotation.processing.ProcessingEnvironment;
33-
import javax.lang.model.element.Element;
34-
import javax.lang.model.element.ExecutableElement;
35-
import javax.lang.model.element.VariableElement;
36-
import java.util.List;
37-
38-
import static com.sun.codemodel.JExpr._new;
39-
import static com.sun.codemodel.JExpr._null;
40-
import static com.sun.codemodel.JExpr.lit;
41-
import static com.sun.codemodel.JMod.FINAL;
42-
import static com.sun.codemodel.JMod.PUBLIC;
43-
import static com.sun.codemodel.JMod.STATIC;
39+
import com.sun.codemodel.JBlock;
40+
import com.sun.codemodel.JClass;
41+
import com.sun.codemodel.JExpression;
42+
import com.sun.codemodel.JFieldVar;
43+
import com.sun.codemodel.JInvocation;
44+
import com.sun.codemodel.JOp;
45+
import com.sun.codemodel.JVar;
4446

4547
public class ReceiverActionHandler extends BaseAnnotationHandler<EReceiverHolder> {
4648

@@ -76,24 +78,45 @@ public void process(Element element, EReceiverHolder holder) throws Exception {
7678
String methodName = element.getSimpleName().toString();
7779

7880
ReceiverAction annotation = element.getAnnotation(ReceiverAction.class);
81+
String[] dataSchemes = annotation.dataSchemes();
7982
String extraKey = annotation.value();
8083
if (extraKey.isEmpty()) {
8184
extraKey = methodName;
8285
}
8386

8487
JFieldVar actionKeyField = createStaticActionField(holder, extraKey, methodName);
85-
addActionInOnReceive(holder, executableElement, methodName, actionKeyField);
88+
JFieldVar dataSchemesField = createStaticDataSchemesField(holder, dataSchemes, methodName);
89+
addActionInOnReceive(holder, executableElement, methodName, actionKeyField, dataSchemesField);
8690
}
8791

8892
private JFieldVar createStaticActionField(EReceiverHolder holder, String extraKey, String methodName) {
8993
String staticFieldName = CaseHelper.camelCaseToUpperSnakeCase("action", methodName, null);
9094
return holder.getGeneratedClass().field(PUBLIC | STATIC | FINAL, classes().STRING, staticFieldName, lit(extraKey));
9195
}
9296

93-
private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) {
97+
private JFieldVar createStaticDataSchemesField(EReceiverHolder holder, String[] dataSchemes, String methodName) {
98+
if (dataSchemes == null || dataSchemes.length == 0) {
99+
return null;
100+
}
101+
JClass listOfStrings = classes().LIST.narrow(classes().STRING);
102+
String staticFieldName = CaseHelper.camelCaseToUpperSnakeCase("dataSchemes", methodName, null);
103+
104+
JInvocation asListInvoke = classes().ARRAYS.staticInvoke("asList");
105+
for (String scheme : dataSchemes) {
106+
asListInvoke.arg(scheme);
107+
}
108+
109+
return holder.getGeneratedClass().field(PUBLIC | STATIC | FINAL, listOfStrings, staticFieldName, asListInvoke);
110+
}
111+
112+
private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField, JFieldVar dataSchemesField) {
94113
// If action match, call the method
95-
JInvocation actionCondition = actionKeyField.invoke("equals").arg(holder.getOnReceiveIntentAction());
96-
JBlock callActionBlock = holder.getOnReceiveBody()._if(actionCondition)._then();
114+
JExpression filterCondition = actionKeyField.invoke("equals").arg(holder.getOnReceiveIntentAction());
115+
if (dataSchemesField != null) {
116+
filterCondition = filterCondition.cand(dataSchemesField.invoke("contains").arg(holder.getOnReceiveIntentDataScheme()));
117+
}
118+
119+
JBlock callActionBlock = holder.getOnReceiveBody()._if(filterCondition)._then();
97120
JExpression receiverRef = holder.getGeneratedClass().staticRef("this");
98121
JInvocation callActionInvocation = receiverRef.invoke(methodName);
99122

@@ -109,10 +132,8 @@ private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement exec
109132
} else if (extraParamClass.equals(classes().INTENT)) {
110133
callActionInvocation.arg(intent);
111134
} else if (param.getAnnotation(ReceiverAction.Extra.class) != null) {
112-
if (extras == null){
113-
extras = callActionBlock.decl(classes().BUNDLE, "extras_",
114-
JOp.cond(intent.invoke("getExtras").ne(_null()),
115-
intent.invoke("getExtras"), _new(classes().BUNDLE)));
135+
if (extras == null) {
136+
extras = callActionBlock.decl(classes().BUNDLE, "extras_", JOp.cond(intent.invoke("getExtras").ne(_null()), intent.invoke("getExtras"), _new(classes().BUNDLE)));
116137
}
117138
callActionInvocation.arg(extraHandler.getExtraValue(param, extras, callActionBlock, holder));
118139
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class EReceiverHolder extends EComponentHolder {
3434
private JFieldVar contextField;
3535
private JBlock onReceiveBody;
3636
private JVar onReceiveIntentAction;
37+
private JVar onReceiveIntentDataScheme;
3738
private JVar onReceiveIntent;
3839
private JVar onReceiveContext;
3940
private JMethod onReceiveMethod;
@@ -67,7 +68,9 @@ private void createOnReceive() {
6768
onReceiveBody.invoke(JExpr._super(), onReceiveMethod).arg(onReceiveContext).arg(onReceiveIntent);
6869

6970
JInvocation getActionInvocation = JExpr.invoke(onReceiveIntent, "getAction");
71+
JInvocation getDataSchemeInvocation = JExpr.invoke(onReceiveIntent, "getScheme");
7072
onReceiveIntentAction = onReceiveBody.decl(classes().STRING, "action", getActionInvocation);
73+
onReceiveIntentDataScheme = onReceiveBody.decl(classes().STRING, "dataScheme", getDataSchemeInvocation);
7174
}
7275

7376
public JMethod getOnReceiveMethod() {
@@ -105,6 +108,13 @@ public JVar getOnReceiveIntentAction() {
105108
return onReceiveIntentAction;
106109
}
107110

111+
public JVar getOnReceiveIntentDataScheme() {
112+
if (onReceiveIntentDataScheme == null) {
113+
createOnReceive();
114+
}
115+
return onReceiveIntentDataScheme;
116+
}
117+
108118
public JFieldVar getContextField() {
109119
if (contextField == null) {
110120
setContextRef();

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/process/ProcessHolder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.InputStream;
2020
import java.io.Serializable;
2121
import java.sql.SQLException;
22+
23+
import java.util.Arrays;
2224
import java.util.Collections;
2325
import java.util.HashMap;
2426
import java.util.HashSet;
@@ -61,6 +63,7 @@ public class Classes {
6163
public final JClass HASH_MAP = refClass(HashMap.class);
6264
public final JClass LIST = refClass(List.class);
6365
public final JClass OBJECT = refClass(Object.class);
66+
public final JClass ARRAYS = refClass(Arrays.class);
6467

6568
/*
6669
* Android

0 commit comments

Comments
 (0)