Skip to content

Commit 28321d0

Browse files
committed
Allow @extra in @eservice
1 parent 46ed0a6 commit 28321d0

10 files changed

Lines changed: 189 additions & 39 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ public void enclosingElementHasEActivity(Element element, AnnotationElements val
169169
hasClassAnnotation(element, enclosingElement, validatedElements, EActivity.class, valid);
170170
}
171171

172+
public void enclosingElementHasEActivityOrEService(Element element, AnnotationElements validatedElements, IsValid valid) {
173+
Element enclosingElement = element.getEnclosingElement();
174+
@SuppressWarnings("unchecked")
175+
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EService.class);
176+
hasOneOfClassAnnotations(element, enclosingElement, validatedElements, validAnnotations, valid);
177+
}
178+
172179
public void enclosingElementHasEActivityOrEFragment(Element element, AnnotationElements validatedElements, IsValid valid) {
173180
Element enclosingElement = element.getEnclosingElement();
174181
@SuppressWarnings("unchecked")
@@ -1181,7 +1188,7 @@ public void validateInterceptors(Element element, IsValid valid) {
11811188
TypeMirror clientHttpRequestInterceptorType = annotationHelper.typeElementFromQualifiedName(CLIENT_HTTP_REQUEST_INTERCEPTOR).asType();
11821189
TypeMirror clientHttpRequestInterceptorTypeErased = annotationHelper.getTypeUtils().erasure(clientHttpRequestInterceptorType);
11831190
List<DeclaredType> interceptors = annotationHelper.extractAnnotationClassArrayParameter(element, annotationHelper.getTarget(), "interceptors");
1184-
if(interceptors == null) {
1191+
if (interceptors == null) {
11851192
return;
11861193
}
11871194
for (DeclaredType interceptorType : interceptors) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,19 @@ public static enum GeneratedClassType {
5353
*/
5454
public JMethod afterSetContentView;
5555
public JBlock extrasNotNullBlock;
56+
public JMethod injectExtrasMethod;
57+
public JMethod injectExtrasSimpleMethod;
5658
public JVar extras;
5759
public JVar resources;
5860

61+
/**
62+
* Only defined in services
63+
*/
64+
public JMethod initStartCommand;
65+
public JVar initStartCommandIntent;
66+
public JMethod onStartCommandMethod;
67+
public JVar onStartCommandMethodIntent;
68+
5969
public JMethod cast;
6070

6171
public JFieldVar handler;

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.sun.codemodel.JDefinedClass;
3838
import com.sun.codemodel.JExpr;
3939
import com.sun.codemodel.JMethod;
40+
import com.sun.codemodel.JVar;
4041

4142
public class EServiceProcessor implements GeneratingElementProcessor {
4243

@@ -80,6 +81,21 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
8081
onCreateBody.invoke(JExpr._super(), onCreate);
8182
}
8283

84+
holder.initStartCommand = holder.generatedClass.method(PRIVATE, codeModel.VOID, "initStartCommand_");
85+
holder.initStartCommandIntent = holder.initStartCommand.param(eBeansHolder.classes().INTENT, "intent");
86+
{
87+
// onStartCommand
88+
holder.onStartCommandMethod = holder.generatedClass.method(PUBLIC, codeModel.INT, "onStartCommand");
89+
holder.onStartCommandMethod.annotate(Override.class);
90+
holder.onStartCommandMethodIntent = holder.onStartCommandMethod.param(eBeansHolder.classes().INTENT, "intent");
91+
JVar flag = holder.onStartCommandMethod.param(codeModel.INT, "flag");
92+
JVar startId = holder.onStartCommandMethod.param(codeModel.INT, "startId");
93+
94+
JBlock onStartCommandBody = holder.onStartCommandMethod.body();
95+
onStartCommandBody.invoke(holder.initStartCommand).arg(holder.onStartCommandMethodIntent);
96+
onStartCommandBody._return(JExpr._super().invoke(holder.onStartCommandMethod).arg(holder.onStartCommandMethodIntent).arg(flag).arg(startId));
97+
}
98+
8399
{
84100
/*
85101
* Setting to null shouldn't be a problem as long as we don't allow
@@ -93,5 +109,4 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
93109
aptCodeModelHelper.addServiceIntentBuilder(codeModel, holder);
94110

95111
}
96-
97112
}

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

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.androidannotations.annotations.Extra;
4141
import org.androidannotations.helper.APTCodeModelHelper;
4242
import org.androidannotations.helper.CaseHelper;
43+
import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
4344
import org.androidannotations.processing.EBeansHolder.Classes;
4445

4546
import com.sun.codemodel.JBlock;
@@ -93,6 +94,10 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) {
9394

9495
if (holder.extras == null) {
9596
injectExtras(holder, codeModel);
97+
injectExtrasSimple(holder, codeModel);
98+
injectOrOverrideSetIntent(holder, codeModel);
99+
injectExtrasOnInit(holder);
100+
injectExtrasOnStartCommand(holder);
96101
}
97102

98103
String staticFieldName;
@@ -178,43 +183,73 @@ private void generateCastMethod(JCodeModel codeModel, EBeanHolder holder) {
178183
}
179184

180185
/**
181-
* Adds call to injectExtras_() in onCreate and setIntent() methods.
186+
* Adds call to injectExtras_(Bundle) in onStartCommand and setIntent()
187+
* methods methods.
182188
*/
183189
private void injectExtras(EBeanHolder holder, JCodeModel codeModel) {
184-
185190
Classes classes = holder.classes();
186191

187-
JMethod injectExtrasMethod = holder.generatedClass.method(PRIVATE, codeModel.VOID, "injectExtras_");
192+
holder.injectExtrasMethod = holder.generatedClass.method(PRIVATE, codeModel.VOID, "injectExtras_");
193+
holder.extras = holder.injectExtrasMethod.param(classes.BUNDLE, "extras_");
194+
195+
JBlock injectExtrasBody = holder.injectExtrasMethod.body();
196+
197+
holder.extrasNotNullBlock = injectExtrasBody._if(holder.extras.ne(_null()))._then();
198+
}
199+
200+
/**
201+
* Adds call to injectExtras_() in onCreate method.
202+
*/
203+
private void injectExtrasSimple(EBeanHolder holder, JCodeModel codeModel) {
204+
if (holder.classType == GeneratedClassType.SERVICE) {
205+
return;
206+
}
188207

189-
overrideSetIntent(holder, codeModel, injectExtrasMethod);
208+
Classes classes = holder.classes();
190209

191-
injectExtrasOnInit(holder, classes.INTENT, injectExtrasMethod);
210+
holder.injectExtrasSimpleMethod = holder.generatedClass.method(PRIVATE, codeModel.VOID, "injectExtras_");
192211

193-
JBlock injectExtrasBody = injectExtrasMethod.body();
212+
JBlock injectExtrasBody = holder.injectExtrasSimpleMethod.body();
194213

195214
JVar intent = injectExtrasBody.decl(classes.INTENT, "intent_", invoke("getIntent"));
196215

197-
holder.extras = injectExtrasBody.decl(classes.BUNDLE, "extras_");
198-
holder.extras.init(intent.invoke("getExtras"));
216+
JVar extras = injectExtrasBody.decl(classes.BUNDLE, "extras_");
217+
extras.init(intent.invoke("getExtras"));
199218

200-
holder.extrasNotNullBlock = injectExtrasBody._if(holder.extras.ne(_null()))._then();
219+
injectExtrasBody.invoke(holder.injectExtrasMethod).arg(extras);
201220
}
202221

203-
private void overrideSetIntent(EBeanHolder holder, JCodeModel codeModel, JMethod initIntentMethod) {
222+
private void injectOrOverrideSetIntent(EBeanHolder holder, JCodeModel codeModel) {
204223
if (holder.intentBuilderClass != null) {
205-
206224
JMethod setIntentMethod = holder.generatedClass.method(PUBLIC, codeModel.VOID, "setIntent");
207-
setIntentMethod.annotate(Override.class);
208225
JVar methodParam = setIntentMethod.param(holder.classes().INTENT, "newIntent");
209226

210227
JBlock setIntentBody = setIntentMethod.body();
211228

212-
setIntentBody.invoke(_super(), setIntentMethod).arg(methodParam);
213-
setIntentBody.invoke(initIntentMethod);
229+
// Override only if it's an activity
230+
if (holder.classType == GeneratedClassType.ACTIVITY) {
231+
setIntentMethod.annotate(Override.class);
232+
setIntentBody.invoke(_super(), setIntentMethod).arg(methodParam);
233+
}
234+
235+
JInvocation extras = methodParam.invoke("getExtras");
236+
setIntentBody.invoke(holder.injectExtrasMethod).arg(extras);
214237
}
215238
}
216239

217-
private void injectExtrasOnInit(EBeanHolder holder, JClass intentClass, JMethod injectExtrasMethod) {
218-
holder.init.body().invoke(injectExtrasMethod);
240+
private void injectExtrasOnInit(EBeanHolder holder) {
241+
if (holder.classType == GeneratedClassType.ACTIVITY) {
242+
holder.init.body().invoke(holder.injectExtrasMethod);
243+
}
244+
}
245+
246+
private void injectExtrasOnStartCommand(EBeanHolder holder) {
247+
if (holder.classType == GeneratedClassType.SERVICE && holder.onStartCommandMethod != null) {
248+
JVar intent = holder.onStartCommandMethodIntent;
249+
JBlock body = holder.initStartCommand.body();
250+
251+
body._if(intent.ne(JExpr._null())) //
252+
._then().invoke(holder.injectExtrasMethod).arg(intent.invoke("getExtras"));
253+
}
219254
}
220255
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/validation/ExtraValidator.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ public Class<? extends Annotation> getTarget() {
4343
public boolean validate(Element element, AnnotationElements validatedElements) {
4444
IsValid valid = new IsValid();
4545

46-
/*
47-
* TODO since we override setIntent(), we should check that the
48-
* setIntent() method can be overridden
49-
*/
50-
51-
validatorHelper.enclosingElementHasEActivity(element, validatedElements, valid);
46+
validatorHelper.enclosingElementHasEActivityOrEService(element, validatedElements, valid);
5247

5348
validatorHelper.isNotPrivate(element, valid);
5449

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<factorypath>
22
<factorypathentry kind="PLUGIN" id="org.eclipse.jst.ws.annotations.core" enabled="true" runInBatchMode="false"/>
3-
<factorypathentry kind="VARJAR" id="M2_REPO/org/androidannotations/androidannotations-api/3.0-SNAPSHOT/androidannotations-api-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
4-
<factorypathentry kind="VARJAR" id="M2_REPO/org/androidannotations/androidannotations/3.0-SNAPSHOT/androidannotations-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
53
<factorypathentry kind="VARJAR" id="M2_REPO/com/sun/codemodel/codemodel/2.4.1/codemodel-2.4.1.jar" enabled="true" runInBatchMode="false"/>
4+
<factorypathentry kind="WKSPJAR" id="/androidannotations-api/target/androidannotations-api-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
5+
<factorypathentry kind="WKSPJAR" id="/androidannotations/target/androidannotations-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
66
</factorypath>

‎AndroidAnnotations/functional-test-1-5/AndroidManifest.xml‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!--
3-
43
Copyright (C) 2010-2012 eBusiness Information, Excilys Group
54
65
Licensed under the Apache License, Version 2.0 (the "License"); you may not
@@ -14,7 +13,6 @@
1413
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1514
License for the specific language governing permissions and limitations under
1615
the License.
17-
1816
-->
1917
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2018
package="org.androidannotations.test15"
@@ -24,13 +22,13 @@
2422
<uses-sdk
2523
android:minSdkVersion="3"
2624
android:targetSdkVersion="8" />
27-
28-
<uses-permission android:name="android.permission.INTERNET"/>
25+
26+
<uses-permission android:name="android.permission.INTERNET" />
2927

3028
<application
3129
android:name="org.androidannotations.test15.roboguice.SampleRoboApplication_"
32-
android:icon="@drawable/icon"
3330
android:debuggable="true"
31+
android:icon="@drawable/icon"
3432
android:label="@string/app_name" >
3533
<activity android:name="org.androidannotations.test15.eviewgroup.CustomFrameLayoutActivity_" >
3634
<intent-filter>
@@ -89,6 +87,9 @@
8987
<service
9088
android:name="org.androidannotations.test15.eservice.MyService_"
9189
android:exported="false" />
90+
<service
91+
android:name="org.androidannotations.test15.eservice.ExtraInjectedService_"
92+
android:exported="false" />
9293
</application>
9394

94-
</manifest>
95+
</manifest>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright (C) 2010-2012 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.test15.eservice;
17+
18+
import java.util.List;
19+
20+
import org.androidannotations.annotations.Background;
21+
import org.androidannotations.annotations.EService;
22+
import org.androidannotations.annotations.Extra;
23+
import org.androidannotations.annotations.Trace;
24+
import org.androidannotations.annotations.UiThread;
25+
import org.androidannotations.test15.CustomData;
26+
27+
import android.app.IntentService;
28+
import android.content.Intent;
29+
import android.os.IBinder;
30+
import android.util.Log;
31+
import android.widget.Toast;
32+
33+
@EService
34+
public class ExtraInjectedService extends IntentService {
35+
36+
@Extra("stringExtra")
37+
String stringExtra;
38+
39+
@Extra("arrayExtra")
40+
CustomData[] arrayExtra;
41+
42+
@Extra("listExtra")
43+
List<String> listExtra;
44+
45+
@Extra("intExtra")
46+
int intExtra;
47+
48+
@Extra("byteArrayExtra")
49+
byte[] byteArrayExtra;
50+
51+
@Extra
52+
String extraWithoutValue;
53+
54+
public ExtraInjectedService() {
55+
super(ExtraInjectedService.class.getSimpleName());
56+
}
57+
58+
@Override
59+
protected void onHandleIntent(Intent intent) {
60+
// Do some stuff...
61+
62+
showToast();
63+
workInBackground();
64+
}
65+
66+
@Trace
67+
@UiThread
68+
void showToast() {
69+
Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();
70+
}
71+
72+
@Trace
73+
@Background
74+
void workInBackground() {
75+
Log.d(ExtraInjectedService.class.getSimpleName(), "Doing some background work.");
76+
}
77+
78+
@Override
79+
public IBinder onBind(Intent intent) {
80+
return null;
81+
}
82+
83+
}

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/eservice/MyService.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515
*/
1616
package org.androidannotations.test15.eservice;
1717

18-
import android.app.IntentService;
19-
import android.app.NotificationManager;
20-
import android.content.Intent;
21-
import android.util.Log;
22-
import android.widget.Toast;
23-
2418
import org.androidannotations.annotations.Background;
2519
import org.androidannotations.annotations.Bean;
2620
import org.androidannotations.annotations.EService;
21+
import org.androidannotations.annotations.Extra;
2722
import org.androidannotations.annotations.OrmLiteDao;
2823
import org.androidannotations.annotations.SystemService;
2924
import org.androidannotations.annotations.Trace;
@@ -33,6 +28,12 @@
3328
import org.androidannotations.test15.ormlite.User;
3429
import org.androidannotations.test15.ormlite.UserDao;
3530

31+
import android.app.IntentService;
32+
import android.app.NotificationManager;
33+
import android.content.Intent;
34+
import android.util.Log;
35+
import android.widget.Toast;
36+
3637
@EService
3738
public class MyService extends IntentService {
3839

@@ -44,6 +45,9 @@ public class MyService extends IntentService {
4445

4546
@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
4647
UserDao userDao;
48+
49+
@Extra
50+
Long someId;
4751

4852
public MyService() {
4953
super(MyService.class.getSimpleName());

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/PrefsActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class PrefsActivity extends Activity {
3232

3333
@Pref
3434
ActivityPrefs_ activityPrefs;
35-
35+
3636
@Pref
3737
ApplicationDefaultPrefs_ applicationDefaultPrefs;
3838

0 commit comments

Comments
 (0)