Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Use this annotation to enhance an Android Service

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To enhance Android Service or Android IntentServices ?

*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface EIntentService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Use this annotation to enhance an Android Service

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy and Paste programming ? :p
Can you enhance the Javadoc please ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha. Damn copy&paste x)

*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface ServiceAction {

String value() default "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this field too


}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.androidannotations.annotations.EApplication;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.EIntentService;
import org.androidannotations.annotations.EProvider;
import org.androidannotations.annotations.EReceiver;
import org.androidannotations.annotations.EService;
Expand Down Expand Up @@ -82,6 +83,7 @@
import org.androidannotations.annotations.SeekBarProgressChange;
import org.androidannotations.annotations.SeekBarTouchStart;
import org.androidannotations.annotations.SeekBarTouchStop;
import org.androidannotations.annotations.ServiceAction;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.annotations.TextChange;
import org.androidannotations.annotations.Touch;
Expand Down Expand Up @@ -141,6 +143,7 @@
import org.androidannotations.processing.EApplicationProcessor;
import org.androidannotations.processing.EBeanProcessor;
import org.androidannotations.processing.EFragmentProcessor;
import org.androidannotations.processing.EIntentServiceProcessor;
import org.androidannotations.processing.EProviderProcessor;
import org.androidannotations.processing.EReceiverProcessor;
import org.androidannotations.processing.EServiceProcessor;
Expand Down Expand Up @@ -178,6 +181,7 @@
import org.androidannotations.processing.SeekBarProgressChangeProcessor;
import org.androidannotations.processing.SeekBarTouchStartProcessor;
import org.androidannotations.processing.SeekBarTouchStopProcessor;
import org.androidannotations.processing.ServiceActionProcessor;
import org.androidannotations.processing.SharedPrefProcessor;
import org.androidannotations.processing.SubscribeProcessor;
import org.androidannotations.processing.SystemServiceProcessor;
Expand Down Expand Up @@ -212,6 +216,7 @@
import org.androidannotations.validation.EApplicationValidator;
import org.androidannotations.validation.EBeanValidator;
import org.androidannotations.validation.EFragmentValidator;
import org.androidannotations.validation.EIntentServiceValidator;
import org.androidannotations.validation.EProviderValidator;
import org.androidannotations.validation.EReceiverValidator;
import org.androidannotations.validation.EServiceValidator;
Expand Down Expand Up @@ -249,6 +254,7 @@
import org.androidannotations.validation.SeekBarProgressChangeValidator;
import org.androidannotations.validation.SeekBarTouchStartValidator;
import org.androidannotations.validation.SeekBarTouchStopValidator;
import org.androidannotations.validation.ServiceActionValidator;
import org.androidannotations.validation.SharedPrefValidator;
import org.androidannotations.validation.SubscribeValidator;
import org.androidannotations.validation.SystemServiceValidator;
Expand Down Expand Up @@ -385,6 +391,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
modelValidator.register(new EApplicationValidator(processingEnv, androidManifest));
modelValidator.register(new EActivityValidator(processingEnv, rClass, androidManifest));
modelValidator.register(new EServiceValidator(processingEnv, androidManifest));
modelValidator.register(new EIntentServiceValidator(processingEnv, androidManifest));
modelValidator.register(new EReceiverValidator(processingEnv, androidManifest));
modelValidator.register(new EProviderValidator(processingEnv, androidManifest));
modelValidator.register(new EFragmentValidator(processingEnv, rClass));
Expand Down Expand Up @@ -439,6 +446,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
modelValidator.register(new SeekBarProgressChangeValidator(processingEnv, rClass));
modelValidator.register(new SeekBarTouchStartValidator(processingEnv, rClass));
modelValidator.register(new SeekBarTouchStopValidator(processingEnv, rClass));
modelValidator.register(new ServiceActionValidator(processingEnv));
/*
* Any view injection or listener binding should occur before
* AfterViewsValidator
Expand Down Expand Up @@ -481,6 +489,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
modelProcessor.register(new EApplicationProcessor());
modelProcessor.register(new EActivityProcessor(processingEnv, rClass));
modelProcessor.register(new EServiceProcessor());
modelProcessor.register(new EIntentServiceProcessor());
modelProcessor.register(new EReceiverProcessor());
modelProcessor.register(new EProviderProcessor());
modelProcessor.register(new EFragmentProcessor(processingEnv, rClass));
Expand Down Expand Up @@ -535,6 +544,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
modelProcessor.register(new SeekBarProgressChangeProcessor(processingEnv, rClass));
modelProcessor.register(new SeekBarTouchStartProcessor(processingEnv, rClass));
modelProcessor.register(new SeekBarTouchStopProcessor(processingEnv, rClass));
modelProcessor.register(new ServiceActionProcessor(processingEnv));
/*
* Any view injection or listener binding should occur before
* AfterViewsProcessor
Expand Down Expand Up @@ -650,6 +660,8 @@ public Set<String> getSupportedAnnotationTypes() {
Bean.class, //
AfterInject.class, //
EService.class, //
EIntentService.class, //
ServiceAction.class, //
EReceiver.class, //
EProvider.class, //
Trace.class, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,25 @@
import static com.sun.codemodel.JMod.PUBLIC;
import static com.sun.codemodel.JMod.STATIC;
import static javax.lang.model.element.ElementKind.CONSTRUCTOR;
import static org.androidannotations.helper.CanonicalNameConstants.PARCELABLE;
import static org.androidannotations.helper.CanonicalNameConstants.SERIALIZABLE;
import static org.androidannotations.helper.CanonicalNameConstants.STRING;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.WildcardType;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

import org.androidannotations.processing.EBeanHolder;
import org.androidannotations.processing.EBeansHolder.Classes;
Expand All @@ -56,6 +63,7 @@
import com.sun.codemodel.JStatement;
import com.sun.codemodel.JTryBlock;
import com.sun.codemodel.JType;
import com.sun.codemodel.JTypeVar;
import com.sun.codemodel.JVar;

public class APTCodeModelHelper {
Expand Down Expand Up @@ -383,4 +391,47 @@ private void addIntentBuilder(JCodeModel codeModel, EBeanHolder holder, boolean
}
}
}

public JInvocation addIntentBuilderPutExtraMethod(JCodeModel codeModel, EBeanHolder holder, APTCodeModelHelper helper, ProcessingEnvironment processingEnv, JMethod method, TypeMirror elementType, String paramName, String extraName) {
boolean castToSerializable = false;
boolean castToParcelable = false;
if (elementType.getKind() == TypeKind.DECLARED) {
Elements elementUtils = processingEnv.getElementUtils();
Types typeUtils = processingEnv.getTypeUtils();
TypeMirror parcelableType = elementUtils.getTypeElement(PARCELABLE).asType();
if (!typeUtils.isSubtype(elementType, parcelableType)) {
TypeMirror stringType = elementUtils.getTypeElement(STRING).asType();
if (!typeUtils.isSubtype(elementType, stringType)) {
castToSerializable = true;
}
} else {
TypeMirror serializableType = elementUtils.getTypeElement(SERIALIZABLE).asType();
if (typeUtils.isSubtype(elementType, serializableType)) {
castToParcelable = true;
}
}
}

JClass paramClass = helper.typeMirrorToJClass(elementType, holder);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameterizedClass or parameterClass ?
Can you choose a more excplicit variable name ?

JVar extraParam = method.param(paramClass, paramName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

JBlock body = method.body();
JInvocation invocation = body.invoke(holder.intentField, "putExtra").arg(extraName);
if (castToSerializable) {
return invocation.arg(cast(holder.classes().SERIALIZABLE, extraParam));
} else if (castToParcelable) {
return invocation.arg(cast(holder.classes().PARCELABLE, extraParam));
}
return invocation.arg(extraParam);
}

public void addCastMethod(JCodeModel codeModel, EBeanHolder holder) {
JType objectType = codeModel._ref(Object.class);
JMethod method = holder.generatedClass.method(JMod.PRIVATE, objectType, "cast_");
JTypeVar genericType = method.generify("T");
method.type(genericType);
JVar objectParam = method.param(objectType, "object");
method.annotate(SuppressWarnings.class).param("value", "unchecked");
method.body()._return(JExpr.cast(genericType, objectParam));
holder.cast = method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public final class CanonicalNameConstants {
public static final String MOTION_EVENT = "android.view.MotionEvent";
public static final String HANDLER = "android.os.Handler";
public static final String SERVICE = "android.app.Service";
public static final String INTENT_SERVICE = "android.app.IntentService";
public static final String BROADCAST_RECEIVER = "android.content.BroadcastReceiver";
public static final String CONTENT_PROVIDER = "android.content.ContentProvider";
public static final String SQLITE_DATABASE = "android.database.sqlite.SQLiteDatabase";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public static String camelCaseToUpperSnakeCase(String camelCase) {
return camelCaseToSnakeCase(camelCase).toUpperCase();
}

public static String camelCaseToUpperSnakeCase(String prefix, String camelCase, String suffix) {
if (prefix != null && !camelCase.startsWith(prefix)) {
camelCase = prefix + "_" + camelCase;
}
if (suffix != null && !camelCase.toLowerCase().endsWith(suffix.toLowerCase())) {
camelCase = camelCase + "_" + suffix;
}
return camelCaseToUpperSnakeCase(camelCase);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.androidannotations.annotations.EApplication;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.EIntentService;
import org.androidannotations.annotations.EProvider;
import org.androidannotations.annotations.EReceiver;
import org.androidannotations.annotations.EService;
Expand All @@ -40,7 +41,7 @@ public abstract class ModelConstants {
public static final List<Class<? extends Annotation>> VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS = asList(EActivity.class, EViewGroup.class, EView.class, EBean.class, EFragment.class);

@SuppressWarnings("unchecked")
public static final List<Class<? extends Annotation>> VALID_ENHANCED_COMPONENT_ANNOTATIONS = asList(EApplication.class, EActivity.class, EViewGroup.class, EView.class, EBean.class, EService.class, EReceiver.class, EProvider.class, EFragment.class);
public static final List<Class<? extends Annotation>> VALID_ENHANCED_COMPONENT_ANNOTATIONS = asList(EApplication.class, EActivity.class, EViewGroup.class, EView.class, EBean.class, EService.class, EIntentService.class, EReceiver.class, EProvider.class, EFragment.class);

private ModelConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.EIntentService;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.Delete;
Expand Down Expand Up @@ -183,6 +184,11 @@ public void enclosingElementHasEFragment(Element element, AnnotationElements val
hasClassAnnotation(element, enclosingElement, validatedElements, EFragment.class, valid);
}

public void enclosingElementHasEIntentService(Element element, AnnotationElements validatedElements, IsValid valid) {
Element enclosingElement = element.getEnclosingElement();
hasClassAnnotation(element, enclosingElement, validatedElements, EIntentService.class, valid);
}

public void hasEActivity(Element element, AnnotationElements validatedElements, IsValid valid) {
hasClassAnnotation(element, element, validatedElements, EActivity.class, valid);
}
Expand Down Expand Up @@ -472,6 +478,10 @@ public void extendsService(Element element, IsValid valid) {
extendsType(element, CanonicalNameConstants.SERVICE, valid);
}

public void extendsIntentService(Element element, IsValid valid) {
extendsType(element, CanonicalNameConstants.INTENT_SERVICE, valid);
}

public void extendsReceiver(Element element, IsValid valid) {
extendsType(element, CanonicalNameConstants.BROADCAST_RECEIVER, valid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public class EBeanHolder {

private ViewChangedHolder viewChangedHolder;

public JVar onHandleIntentIntent;
public JBlock onHandleIntentBody;

/**
* Only defined in beans that implement {@link HasViews}
*/
Expand Down
Loading