Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010-2011 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 com.googlecode.androidannotations.processing;
 
 import static com.sun.codemodel.JExpr._new;
 import static com.sun.codemodel.JExpr._this;
 import static com.sun.codemodel.JMod.FINAL;
 import static com.sun.codemodel.JMod.PRIVATE;
 import static com.sun.codemodel.JMod.PUBLIC;
 import static com.sun.codemodel.JMod.STATIC;
 
 import java.util.List;
 import java.util.Set;
 
 
 
 public class EActivityProcessor extends AnnotationHelper implements ElementProcessor {
 
 	private final IRClass rClass;
 
 	public EActivityProcessor(ProcessingEnvironment processingEnvIRClass rClass) {
 		super(processingEnv);
 		this. = rClass;
 	}
 
 	public Class<? extends AnnotationgetTarget() {
 		return EActivity.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder activitiesHolderthrows Exception {
 
 		EBeanHolder holder = activitiesHolder.create(element);
 
 		TypeElement typeElement = (TypeElementelement;
 
 		// Activity
 		String annotatedActivityQualifiedName = typeElement.getQualifiedName().toString();
 
 		String subActivityQualifiedName = annotatedActivityQualifiedName + .;
 
 		int modifiers;
 		boolean isAbstract = element.getModifiers().contains(.);
 		if (isAbstract) {
 			modifiers = . | .;
 		} else {
 			modifiers = . | .;
 		}
 
 		holder.eBean = codeModel._class(modifierssubActivityQualifiedName.);
 
 		JClass annotatedActivity = codeModel.directClass(annotatedActivityQualifiedName);
 
 		holder.eBean._extends(annotatedActivity);
 
		holder.contextRef = _this();
		// onCreate
		int onCreateVisibility;
		if (isAbstract) {
			onCreateVisibility = inheritedOnCreateVisibility(typeElement);
else {
			onCreateVisibility = ;
		}
		JMethod onCreate = holder.eBean.method(onCreateVisibilitycodeModel.VOID"onCreate");
		onCreate.annotate(Override.class);
		JClass bundleClass = holder.refClass("android.os.Bundle");
		// beforeSetContentView
		holder.init = holder.eBean.method(codeModel.VOID"init_");
		holder.beforeCreateSavedInstanceStateParam = holder.init.param(bundleClass"savedInstanceState");
		{
			// init if activity
			holder.initIfActivityBody = holder.init.body();
			holder.initActivityRef = _this();
		}
		// afterSetContentView
		holder.afterSetContentView = holder.eBean.method(codeModel.VOID"afterSetContentView_");
		JVar onCreateSavedInstanceState = onCreate.param(bundleClass"savedInstanceState");
		JBlock onCreateBody = onCreate.body();
		onCreateBody.invoke(holder.init).arg(onCreateSavedInstanceState);
		onCreateBody.invoke(JExpr._super(), onCreate).arg(onCreateSavedInstanceState);
		EActivity layoutAnnotation = element.getAnnotation(EActivity.class);
		int layoutIdValue = layoutAnnotation.value();
		JFieldRef contentViewId;
		if (layoutIdValue != .) {
			IRInnerClass rInnerClass = .get(.);
			contentViewId = rInnerClass.getIdStaticRef(layoutIdValueholder);
else {
			contentViewId = null;
		}
		if (contentViewId != null) {
			onCreateBody.invoke("setContentView").arg(contentViewId);
		}
		// Overriding setContentView (with layout id param)
		JClass viewClass = holder.refClass("android.view.View");
		JClass layoutParamsClass = holder.refClass("android.view.ViewGroup.LayoutParams");
		setContentViewMethod(codeModelholdernew JType[] { codeModel.INT }, new String[] { "layoutResID" });
		setContentViewMethod(codeModelholdernew JType[] { viewClasslayoutParamsClass }, new String[] { "view""params" });
		setContentViewMethod(codeModelholdernew JType[] { viewClass }, new String[] { "view" });
		// Handling onBackPressed
		if (hasOnBackPressedMethod(typeElement)) {
			JMethod onKeyDownMethod = holder.eBean.method(codeModel.BOOLEAN"onKeyDown");
			onKeyDownMethod.annotate(Override.class);
			JVar keyCodeParam = onKeyDownMethod.param(codeModel.INT"keyCode");
			JClass keyEventClass = holder.refClass("android.view.KeyEvent");
			JVar eventParam = onKeyDownMethod.param(keyEventClass"event");
			JClass versionHelperClass = codeModel.ref(SdkVersionHelper.class);
			JInvocation sdkInt = versionHelperClass.staticInvoke("getSdkInt");
			JBlock onKeyDownBody = onKeyDownMethod.body();
			onKeyDownBody._if//
					sdkInt.lt(JExpr.lit(5)) //
							.cand(keyCodeParam.eq(keyEventClass.staticRef("KEYCODE_BACK"))) //
							.cand(eventParam.invoke("getRepeatCount").eq(JExpr.lit(0)))) //
					._then() //
					.invoke("onBackPressed");
			onKeyDownBody._return//
					JExpr._super().invoke(onKeyDownMethod//
							.arg(keyCodeParam//
							.arg(eventParam));
		}
		if (!isAbstract) {
			JClass contextClass = holder.refClass("android.content.Context");
			JClass intentClass = holder.refClass("android.content.Intent");
			{
				holder.intentBuilderClass = holder.eBean._class( | "IntentBuilder_");
				JFieldVar contextField = holder.intentBuilderClass.field(contextClass"context_");
				holder.intentField = holder.intentBuilderClass.field( | intentClass"intent_");
				{
					// Constructor
					JMethod constructor = holder.intentBuilderClass.constructor(.);
					JVar constructorContextParam = constructor.param(contextClass"context");
					JBlock constructorBody = constructor.body();
					constructorBody.assign(contextFieldconstructorContextParam);
					constructorBody.assign(holder.intentField_new(intentClass).arg(constructorContextParam).arg(holder.eBean.dotclass()));
				}
				{
					// get()
					JMethod method = holder.intentBuilderClass.method(intentClass"get");
					method.body()._return(holder.intentField);
				}
				{
					// flags()
					JMethod method = holder.intentBuilderClass.method(holder.intentBuilderClass"flags");
					JVar flagsParam = method.param(codeModel.INT"flags");
					JBlock body = method.body();
					body.invoke(holder.intentField"setFlags").arg(flagsParam);
					body._return(_this());
				}
				{
					// start
					JMethod method = holder.intentBuilderClass.method(codeModel.VOID"start");
					method.body().invoke(contextField"startActivity").arg(holder.intentField);
				}
				{
					// intent()
					JMethod method = holder.eBean.method( | holder.intentBuilderClass"intent");
					JVar contextParam = method.param(contextClass"context");
					method.body()._return(_new(holder.intentBuilderClass).arg(contextParam));
				}
			}
		}
	}
	private void setContentViewMethod(JCodeModel codeModelEBeanHolder holderJType[] paramTypesString[] paramNames) {
		JMethod method = holder.eBean.method(.codeModel.VOID"setContentView");
		method.annotate(Override.class);
		ArrayList<JVarparams = new ArrayList<JVar>();
		for (int i = 0; i < paramTypes.lengthi++) {
			JVar param = method.param(paramTypes[i], paramNames[i]);
			params.add(param);
		}
		JBlock body = method.body();
		JInvocation superCall = body.invoke(JExpr._super(), method);
		for (JVar arg : params) {
			superCall.arg(arg);
		}
		body.invoke(holder.afterSetContentView);
	}
	private int inheritedOnCreateVisibility(TypeElement activityElement) {
		List<? extends ElementallMembers = getElementUtils().getAllMembers(activityElement);
		List<ExecutableElementactivityInheritedMethods = ElementFilter.methodsIn(allMembers);
		for (ExecutableElement activityInheritedMethod : activityInheritedMethods) {
			if (isOnCreateMethod(activityInheritedMethod)) {
				Set<Modifiermodifiers = activityInheritedMethod.getModifiers();
				for (Modifier modifier : modifiers) {
					if (modifier == .) {
						return .;
else if (modifier == .) {
						return .;
					}
				}
				return .;
			}
		}
		return ;
	}
	private boolean isOnCreateMethod(ExecutableElement method) {
		List<? extends VariableElementparameters = method.getParameters();
		return method.getSimpleName().toString().equals("onCreate"//
				&& parameters.size() == 1 //
				&& parameters.get(0).asType().toString().equals("android.os.Bundle"//
		;
	}
	private boolean hasOnBackPressedMethod(TypeElement activityElement) {
		List<? extends ElementallMembers = getElementUtils().getAllMembers(activityElement);
		List<ExecutableElementactivityInheritedMethods = ElementFilter.methodsIn(allMembers);
		for (ExecutableElement activityInheritedMethod : activityInheritedMethods) {
			if (isOnBackPressedMethod(activityInheritedMethod)) {
				return true;
			}
		}
		return false;
	}
	private boolean isOnBackPressedMethod(ExecutableElement method) {
		return method.getSimpleName().toString().equals("onBackPressed"//
				&& method.getThrownTypes().size() == 0 //
				&& method.getParameters().size() == 0 //
		;
	}
New to GrepCode? Check out our FAQ X