1515 */
1616package 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+
2532import org .androidannotations .annotations .ReceiverAction ;
2633import org .androidannotations .helper .APTCodeModelHelper ;
2734import org .androidannotations .helper .CaseHelper ;
2835import org .androidannotations .holder .EReceiverHolder ;
2936import org .androidannotations .model .AnnotationElements ;
3037import 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
4547public 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 }
0 commit comments