Skip to content

Commit be954c9

Browse files
committed
Implement onHandleIntent for @EIntentService according to issue androidannotations#572
1 parent 60a82e7 commit be954c9

10 files changed

Lines changed: 465 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Use this annotation to enhance an Android Service
25+
*/
26+
@Retention(RetentionPolicy.CLASS)
27+
@Target(ElementType.TYPE)
28+
public @interface EIntentService {
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Use this annotation to enhance an Android Service
25+
*/
26+
@Retention(RetentionPolicy.CLASS)
27+
@Target(ElementType.METHOD)
28+
public @interface ServiceAction {
29+
30+
String value() default "";
31+
32+
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.androidannotations.annotations.EApplication;
5353
import org.androidannotations.annotations.EBean;
5454
import org.androidannotations.annotations.EFragment;
55+
import org.androidannotations.annotations.EIntentService;
5556
import org.androidannotations.annotations.EProvider;
5657
import org.androidannotations.annotations.EReceiver;
5758
import org.androidannotations.annotations.EService;
@@ -82,6 +83,7 @@
8283
import org.androidannotations.annotations.SeekBarProgressChange;
8384
import org.androidannotations.annotations.SeekBarTouchStart;
8485
import org.androidannotations.annotations.SeekBarTouchStop;
86+
import org.androidannotations.annotations.ServiceAction;
8587
import org.androidannotations.annotations.SystemService;
8688
import org.androidannotations.annotations.TextChange;
8789
import org.androidannotations.annotations.Touch;
@@ -141,6 +143,7 @@
141143
import org.androidannotations.processing.EApplicationProcessor;
142144
import org.androidannotations.processing.EBeanProcessor;
143145
import org.androidannotations.processing.EFragmentProcessor;
146+
import org.androidannotations.processing.EIntentServiceProcessor;
144147
import org.androidannotations.processing.EProviderProcessor;
145148
import org.androidannotations.processing.EReceiverProcessor;
146149
import org.androidannotations.processing.EServiceProcessor;
@@ -178,6 +181,7 @@
178181
import org.androidannotations.processing.SeekBarProgressChangeProcessor;
179182
import org.androidannotations.processing.SeekBarTouchStartProcessor;
180183
import org.androidannotations.processing.SeekBarTouchStopProcessor;
184+
import org.androidannotations.processing.ServiceActionProcessor;
181185
import org.androidannotations.processing.SharedPrefProcessor;
182186
import org.androidannotations.processing.SubscribeProcessor;
183187
import org.androidannotations.processing.SystemServiceProcessor;
@@ -212,6 +216,7 @@
212216
import org.androidannotations.validation.EApplicationValidator;
213217
import org.androidannotations.validation.EBeanValidator;
214218
import org.androidannotations.validation.EFragmentValidator;
219+
import org.androidannotations.validation.EIntentServiceValidator;
215220
import org.androidannotations.validation.EProviderValidator;
216221
import org.androidannotations.validation.EReceiverValidator;
217222
import org.androidannotations.validation.EServiceValidator;
@@ -249,6 +254,7 @@
249254
import org.androidannotations.validation.SeekBarProgressChangeValidator;
250255
import org.androidannotations.validation.SeekBarTouchStartValidator;
251256
import org.androidannotations.validation.SeekBarTouchStopValidator;
257+
import org.androidannotations.validation.ServiceActionValidator;
252258
import org.androidannotations.validation.SharedPrefValidator;
253259
import org.androidannotations.validation.SubscribeValidator;
254260
import org.androidannotations.validation.SystemServiceValidator;
@@ -385,6 +391,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
385391
modelValidator.register(new EApplicationValidator(processingEnv, androidManifest));
386392
modelValidator.register(new EActivityValidator(processingEnv, rClass, androidManifest));
387393
modelValidator.register(new EServiceValidator(processingEnv, androidManifest));
394+
modelValidator.register(new EIntentServiceValidator(processingEnv, androidManifest));
388395
modelValidator.register(new EReceiverValidator(processingEnv, androidManifest));
389396
modelValidator.register(new EProviderValidator(processingEnv, androidManifest));
390397
modelValidator.register(new EFragmentValidator(processingEnv, rClass));
@@ -439,6 +446,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
439446
modelValidator.register(new SeekBarProgressChangeValidator(processingEnv, rClass));
440447
modelValidator.register(new SeekBarTouchStartValidator(processingEnv, rClass));
441448
modelValidator.register(new SeekBarTouchStopValidator(processingEnv, rClass));
449+
modelValidator.register(new ServiceActionValidator(processingEnv));
442450
/*
443451
* Any view injection or listener binding should occur before
444452
* AfterViewsValidator
@@ -481,6 +489,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
481489
modelProcessor.register(new EApplicationProcessor());
482490
modelProcessor.register(new EActivityProcessor(processingEnv, rClass));
483491
modelProcessor.register(new EServiceProcessor());
492+
modelProcessor.register(new EIntentServiceProcessor());
484493
modelProcessor.register(new EReceiverProcessor());
485494
modelProcessor.register(new EProviderProcessor());
486495
modelProcessor.register(new EFragmentProcessor(processingEnv, rClass));
@@ -535,6 +544,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
535544
modelProcessor.register(new SeekBarProgressChangeProcessor(processingEnv, rClass));
536545
modelProcessor.register(new SeekBarTouchStartProcessor(processingEnv, rClass));
537546
modelProcessor.register(new SeekBarTouchStopProcessor(processingEnv, rClass));
547+
modelProcessor.register(new ServiceActionProcessor(processingEnv));
538548
/*
539549
* Any view injection or listener binding should occur before
540550
* AfterViewsProcessor
@@ -650,6 +660,8 @@ public Set<String> getSupportedAnnotationTypes() {
650660
Bean.class, //
651661
AfterInject.class, //
652662
EService.class, //
663+
EIntentService.class, //
664+
ServiceAction.class, //
653665
EReceiver.class, //
654666
EProvider.class, //
655667
Trace.class, //

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public final class CanonicalNameConstants {
9191
public static final String MOTION_EVENT = "android.view.MotionEvent";
9292
public static final String HANDLER = "android.os.Handler";
9393
public static final String SERVICE = "android.app.Service";
94+
public static final String INTENT_SERVICE = "android.app.IntentService";
9495
public static final String BROADCAST_RECEIVER = "android.content.BroadcastReceiver";
9596
public static final String CONTENT_PROVIDER = "android.content.ContentProvider";
9697
public static final String SQLITE_DATABASE = "android.database.sqlite.SQLiteDatabase";

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.androidannotations.annotations.EActivity;
5454
import org.androidannotations.annotations.EBean;
5555
import org.androidannotations.annotations.EFragment;
56+
import org.androidannotations.annotations.EIntentService;
5657
import org.androidannotations.annotations.Trace;
5758
import org.androidannotations.annotations.ViewById;
5859
import org.androidannotations.annotations.rest.Delete;
@@ -183,6 +184,11 @@ public void enclosingElementHasEFragment(Element element, AnnotationElements val
183184
hasClassAnnotation(element, enclosingElement, validatedElements, EFragment.class, valid);
184185
}
185186

187+
public void enclosingElementHasEIntentService(Element element, AnnotationElements validatedElements, IsValid valid) {
188+
Element enclosingElement = element.getEnclosingElement();
189+
hasClassAnnotation(element, enclosingElement, validatedElements, EIntentService.class, valid);
190+
}
191+
186192
public void hasEActivity(Element element, AnnotationElements validatedElements, IsValid valid) {
187193
hasClassAnnotation(element, element, validatedElements, EActivity.class, valid);
188194
}
@@ -472,6 +478,10 @@ public void extendsService(Element element, IsValid valid) {
472478
extendsType(element, CanonicalNameConstants.SERVICE, valid);
473479
}
474480

481+
public void extendsIntentService(Element element, IsValid valid) {
482+
extendsType(element, CanonicalNameConstants.INTENT_SERVICE, valid);
483+
}
484+
475485
public void extendsReceiver(Element element, IsValid valid) {
476486
extendsType(element, CanonicalNameConstants.BROADCAST_RECEIVER, valid);
477487
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeanHolder.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ public class EBeanHolder {
130130

131131
private ViewChangedHolder viewChangedHolder;
132132

133+
public JVar onHandleIntentIntent;
134+
public JBlock onHandleIntentBody;
135+
133136
/**
134137
* Only defined in beans that implement {@link HasViews}
135138
*/
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.processing;
17+
18+
import static com.sun.codemodel.JExpr._this;
19+
import static com.sun.codemodel.JMod.FINAL;
20+
import static com.sun.codemodel.JMod.PRIVATE;
21+
import static com.sun.codemodel.JMod.PUBLIC;
22+
23+
import javax.lang.model.element.Element;
24+
import javax.lang.model.element.TypeElement;
25+
26+
import org.androidannotations.annotations.EIntentService;
27+
import org.androidannotations.helper.APTCodeModelHelper;
28+
import org.androidannotations.helper.ModelConstants;
29+
30+
import com.sun.codemodel.ClassType;
31+
import com.sun.codemodel.JBlock;
32+
import com.sun.codemodel.JClass;
33+
import com.sun.codemodel.JCodeModel;
34+
import com.sun.codemodel.JDefinedClass;
35+
import com.sun.codemodel.JExpr;
36+
import com.sun.codemodel.JMethod;
37+
import com.sun.codemodel.JVar;
38+
39+
public class EIntentServiceProcessor extends EServiceProcessor {
40+
41+
private final APTCodeModelHelper aptCodeModelHelper;
42+
43+
public EIntentServiceProcessor() {
44+
aptCodeModelHelper = new APTCodeModelHelper();
45+
}
46+
47+
@Override
48+
public String getTarget() {
49+
return EIntentService.class.getName();
50+
}
51+
52+
@Override
53+
public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHolder) throws Exception {
54+
55+
TypeElement typeElement = (TypeElement) element;
56+
57+
String annotatedComponentQualifiedName = typeElement.getQualifiedName().toString();
58+
59+
String generatedComponentQualifiedName = annotatedComponentQualifiedName + ModelConstants.GENERATION_SUFFIX;
60+
61+
JDefinedClass generatedClass = codeModel._class(PUBLIC | FINAL, generatedComponentQualifiedName, ClassType.CLASS);
62+
63+
EBeanHolder holder = eBeansHolder.create(element, EIntentService.class, generatedClass);
64+
65+
JClass annotatedComponent = codeModel.directClass(annotatedComponentQualifiedName);
66+
67+
holder.generatedClass._extends(annotatedComponent);
68+
69+
holder.contextRef = _this();
70+
71+
JMethod init = holder.generatedClass.method(PRIVATE, codeModel.VOID, "init_");
72+
holder.initBody = init.body();
73+
{
74+
// onCreate
75+
JMethod onCreate = holder.generatedClass.method(PUBLIC, codeModel.VOID, "onCreate");
76+
onCreate.annotate(Override.class);
77+
JBlock onCreateBody = onCreate.body();
78+
onCreateBody.invoke(init);
79+
onCreateBody.invoke(JExpr._super(), onCreate);
80+
}
81+
82+
{
83+
// onHandleIntent
84+
JMethod onHandleIntent = holder.generatedClass.method(PUBLIC, codeModel.VOID, "onHandleIntent");
85+
JVar intent = onHandleIntent.param(eBeansHolder.classes().INTENT, "intent");
86+
onHandleIntent.annotate(Override.class);
87+
JBlock onHandleIntentBody = onHandleIntent.body();
88+
aptCodeModelHelper.callSuperMethod(onHandleIntent, holder, onHandleIntentBody);
89+
90+
holder.onHandleIntentIntent = intent;
91+
holder.onHandleIntentBody = onHandleIntentBody;
92+
}
93+
94+
{
95+
/*
96+
* Setting to null shouldn't be a problem as long as we don't allow
97+
*
98+
* @App and @Extra on this component
99+
*/
100+
holder.initIfActivityBody = null;
101+
holder.initActivityRef = null;
102+
}
103+
104+
aptCodeModelHelper.addServiceIntentBuilder(codeModel, holder);
105+
106+
}
107+
}

0 commit comments

Comments
 (0)