Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 IntentService
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface EIntentService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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;

import android.app.IntentService;

/**
* Should be used on a method that must respond to a specific action in an
* enhanced {@link IntentService}. The method name will be used as action name
* unless the {@link #value()} field is set.
* <p/ >
* The method signature (with attributes) will be a part of the IntentBuilder
* generated for the {@link EIntentService}.
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface ServiceAction {

/**
* Define the action's name. If this field isn't set the annotated method
* name will be used.
*/
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 @@ -54,6 +54,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 @@ -85,6 +86,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 @@ -147,6 +149,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 @@ -184,6 +187,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 @@ -219,6 +223,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 @@ -256,6 +261,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 @@ -416,6 +422,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 @@ -471,6 +478,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 @@ -513,6 +521,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
modelProcessor.register(new EApplicationProcessor());
modelProcessor.register(new EActivityProcessor(processingEnv, rClass));
modelProcessor.register(new EServiceProcessor(processingEnv));
modelProcessor.register(new EIntentServiceProcessor(processingEnv));
modelProcessor.register(new EReceiverProcessor());
modelProcessor.register(new EProviderProcessor());
modelProcessor.register(new EFragmentProcessor(processingEnv, rClass));
Expand Down Expand Up @@ -568,6 +577,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 @@ -677,6 +687,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,19 +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;

Expand All @@ -54,6 +60,7 @@
import com.sun.codemodel.JMod;
import com.sun.codemodel.JStatement;
import com.sun.codemodel.JType;
import com.sun.codemodel.JTypeVar;
import com.sun.codemodel.JVar;

public class APTCodeModelHelper {
Expand Down Expand Up @@ -238,7 +245,7 @@ public JDefinedClass createDelegatingAnonymousRunnableClass(EBeanHolder holder,
JMethod runMethod = anonymousRunnableClass.method(JMod.PUBLIC, codeModel.VOID, "run");
runMethod.annotate(Override.class);

runMethod.body().add( previousMethodBody );
runMethod.body().add(previousMethodBody);

return anonymousRunnableClass;
}
Expand Down Expand Up @@ -415,6 +422,49 @@ private void addIntentBuilder(JCodeModel codeModel, EBeanHolder holder, Annotati
}
}

public JInvocation addIntentBuilderPutExtraMethod(JCodeModel codeModel, EBeanHolder holder, APTCodeModelHelper helper, ProcessingEnvironment processingEnv, JMethod method, TypeMirror elementType, String parameterName, 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 parameterClass = helper.typeMirrorToJClass(elementType, holder);
JVar extraParameterVar = method.param(parameterClass, parameterName);
JBlock body = method.body();
JInvocation invocation = body.invoke(holder.intentField, "putExtra").arg(extraName);
if (castToSerializable) {
return invocation.arg(cast(holder.classes().SERIALIZABLE, extraParameterVar));
} else if (castToParcelable) {
return invocation.arg(cast(holder.classes().PARCELABLE, extraParameterVar));
}
return invocation.arg(extraParameterVar);
}

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;
}

private JFieldVar addIntentBuilderFragmentConstructor(EBeanHolder holder, JClass fragmentClass, String fieldName, JFieldVar contextField) {

JFieldVar fragmentField = holder.intentBuilderClass.field(PRIVATE, fragmentClass, fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ public boolean enclosingElementHasEnhancedComponentAnnotation(Element element) {
return hasOneOfClassAnnotations(enclosingElement, VALID_ENHANCED_COMPONENT_ANNOTATIONS);
}

public boolean hasOneOfClassAnnotations(Element element, Class<? extends Annotation> validAnnotation) {
List<Class<? extends Annotation>> annotations = new ArrayList<Class<? extends Annotation>>();
annotations.add(validAnnotation);
return hasOneOfClassAnnotations(element, annotations);
}

public boolean hasOneOfClassAnnotations(Element element, List<Class<? extends Annotation>> validAnnotations) {
for (Class<? extends Annotation> validAnnotation : validAnnotations) {
if (element.getAnnotation(validAnnotation) != null) {
Expand Down
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 @@ -34,6 +34,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
Expand All @@ -53,6 +54,8 @@
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.EIntentService;
import org.androidannotations.annotations.ServiceAction;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.Delete;
Expand Down Expand Up @@ -205,6 +208,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 @@ -494,6 +502,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 Expand Up @@ -1178,4 +1190,23 @@ public void validateInterceptors(Element element, IsValid valid) {
}
}

public void hasNotMultipleAnnotatedMethodWithSameName(Element element, IsValid valid, Class<? extends Annotation> annotation) {
Set<String> actionNames = new TreeSet<String>();

List<? extends Element> enclosedElements = element.getEnclosedElements();
for (Element enclosedElement : enclosedElements) {
if (enclosedElement.getKind() != ElementKind.METHOD || !annotationHelper.hasOneOfClassAnnotations(enclosedElement, annotation)) {
continue;
}

String enclosedElementName = enclosedElement.getSimpleName().toString();
if (actionNames.contains(enclosedElementName)) {
valid.invalidate();
annotationHelper.printError(enclosedElement, "The " + TargetAnnotationHelper.annotationName(ServiceAction.class) + " annotated method must have unique name even if the signature is not the same");
} else {
actionNames.add(enclosedElementName);
}
}
}

}
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.