Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.androidannotations.internal.core.handler.BeforeTextChangeHandler;
import org.androidannotations.internal.core.handler.CheckedChangeHandler;
import org.androidannotations.internal.core.handler.ClickHandler;
import org.androidannotations.internal.core.handler.ColorResHandler;
import org.androidannotations.internal.core.handler.ColorStateListResHandler;
import org.androidannotations.internal.core.handler.CustomTitleHandler;
import org.androidannotations.internal.core.handler.DefaultResHandler;
import org.androidannotations.internal.core.handler.DrawableResHandler;
Expand Down Expand Up @@ -152,6 +154,10 @@ public List<AnnotationHandler<?>> getHandlers(AndroidAnnotationsEnvironment andr
for (AndroidRes androidRes : AndroidRes.values()) {
if (androidRes == AndroidRes.ANIMATION) {
annotationHandlers.add(new AnimationResHandler(androidAnnotationEnv));
} else if (androidRes == AndroidRes.COLOR) {
annotationHandlers.add(new ColorResHandler(androidAnnotationEnv));
} else if (androidRes == AndroidRes.COLOR_STATE_LIST) {
annotationHandlers.add(new ColorStateListResHandler(androidAnnotationEnv));
} else if (androidRes == AndroidRes.DRAWABLE) {
annotationHandlers.add(new DrawableResHandler(androidAnnotationEnv));
} else if (androidRes == AndroidRes.HTML) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2010-2015 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.internal.core.handler;

import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.internal.core.model.AndroidRes;

public class ColorResHandler extends ContextCompatAwareResHandler {

private static final int MIN_SDK_WITH_CONTEXT_GET_COLOR = 23;
private static final String MIN_SDK_PLATFORM_NAME = "M";

public ColorResHandler(AndroidAnnotationsEnvironment environment) {
super(AndroidRes.COLOR, environment, MIN_SDK_WITH_CONTEXT_GET_COLOR, MIN_SDK_PLATFORM_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2010-2015 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.internal.core.handler;

import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.internal.core.model.AndroidRes;

public class ColorStateListResHandler extends ContextCompatAwareResHandler {

private static final int MIN_SDK_WITH_CONTEXT_GET_COLOR_STATE_LIST = 23;
private static final String MIN_SDK_PLATFORM_NAME = "M";

public ColorStateListResHandler(AndroidAnnotationsEnvironment environment) {
super(AndroidRes.COLOR_STATE_LIST, environment, MIN_SDK_WITH_CONTEXT_GET_COLOR_STATE_LIST, MIN_SDK_PLATFORM_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (C) 2010-2015 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.internal.core.handler;

import static com.helger.jcodemodel.JExpr.invoke;
import static com.helger.jcodemodel.JExpr.ref;

import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.internal.core.model.AndroidRes;

import com.helger.jcodemodel.JBlock;
import com.helger.jcodemodel.JConditional;
import com.helger.jcodemodel.JFieldRef;
import com.helger.jcodemodel.JVar;

abstract class ContextCompatAwareResHandler extends AbstractResHandler {

private final int minSdkWithMethod;
private final String minSdkPlatformName;

ContextCompatAwareResHandler(AndroidRes androidRes, AndroidAnnotationsEnvironment environment, int minSdkWithMethod, String minSdkPlatformName) {
super(androidRes, environment);

this.minSdkWithMethod = minSdkWithMethod;
this.minSdkPlatformName = minSdkPlatformName;
}

@Override
protected void makeCall(String fieldName, EComponentHolder holder, JBlock methodBody, JFieldRef idRef) {
JFieldRef ref = ref(fieldName);
if (hasContextCompatInClasspath()) {
methodBody.assign(ref, getClasses().CONTEXT_COMPAT.staticInvoke(androidRes.getResourceMethodName()).arg(holder.getContextRef()).arg(idRef));
} else if (shouldUseContextMethod() && !hasContextCompatInClasspath()) {
methodBody.assign(ref, holder.getContextRef().invoke(androidRes.getResourceMethodName()).arg(idRef));
} else if (!shouldUseContextMethod() && hasTargetMethodInContext() && !hasContextCompatInClasspath()) {
createCallWithIfGuard(holder, ref, methodBody, idRef);
} else {
methodBody.assign(ref, invoke(holder.getResourcesRef(), androidRes.getResourceMethodName()).arg(idRef));
}
}

private boolean shouldUseContextMethod() {
return getEnvironment().getAndroidManifest().getMinSdkVersion() >= minSdkWithMethod;
}

private boolean hasTargetMethodInContext() {
TypeElement context = getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT);

return hasTargetMethod(context);
}

private void createCallWithIfGuard(EComponentHolder holder, JFieldRef ref, JBlock methodBody, JFieldRef idRef) {
JVar resourcesRef = holder.getResourcesRef();
JConditional guardIf = methodBody._if(getClasses().BUILD_VERSION.staticRef("SDK_INT").gte(getClasses().BUILD_VERSION_CODES.staticRef(minSdkPlatformName)));
JBlock ifBlock = guardIf._then();
ifBlock.assign(ref, holder.getContextRef().invoke(androidRes.getResourceMethodName()).arg(idRef));

JBlock elseBlock = guardIf._else();
elseBlock.assign(ref, resourcesRef.invoke(androidRes.getResourceMethodName()).arg(idRef));
}

private boolean hasTargetMethod(TypeElement type) {
if (type == null) {
return false;
}

List<? extends Element> allMembers = getProcessingEnvironment().getElementUtils().getAllMembers(type);
for (ExecutableElement element : ElementFilter.methodsIn(allMembers)) {
if (element.getSimpleName().contentEquals(androidRes.getResourceMethodName())) {
return true;
}
}
return false;
}

protected boolean hasContextCompatInClasspath() {
return getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT_COMPAT) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,83 +15,15 @@
*/
package org.androidannotations.internal.core.handler;

import static com.helger.jcodemodel.JExpr.invoke;
import static com.helger.jcodemodel.JExpr.ref;

import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.internal.core.model.AndroidRes;

import com.helger.jcodemodel.JBlock;
import com.helger.jcodemodel.JConditional;
import com.helger.jcodemodel.JFieldRef;
import com.helger.jcodemodel.JVar;

public class DrawableResHandler extends AbstractResHandler {
public class DrawableResHandler extends ContextCompatAwareResHandler {

private static final int MIN_SDK_WITH_CONTEXT_GET_DRAWABLE = 21;
private static final String MIN_SDK_PLATFORM_NAME = "LOLLIPOP";

public DrawableResHandler(AndroidAnnotationsEnvironment environment) {
super(AndroidRes.DRAWABLE, environment);
}

@Override
protected void makeCall(String fieldName, EComponentHolder holder, JBlock methodBody, JFieldRef idRef) {
JFieldRef ref = ref(fieldName);
if (hasContextCompatInClasspath()) {
methodBody.assign(ref, getClasses().CONTEXT_COMPAT.staticInvoke("getDrawable").arg(holder.getContextRef()).arg(idRef));
} else if (shouldUseContextGetDrawableMethod() && !hasContextCompatInClasspath()) {
methodBody.assign(ref, holder.getContextRef().invoke("getDrawable").arg(idRef));
} else if (!shouldUseContextGetDrawableMethod() && hasGetDrawableInContext() && !hasContextCompatInClasspath()) {
createCallWithIfGuard(holder, ref, methodBody, idRef);
} else {
methodBody.assign(ref, invoke(holder.getResourcesRef(), androidRes.getResourceMethodName()).arg(idRef));
}
}

private boolean hasContextCompatInClasspath() {
return getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT_COMPAT) != null;
}

private boolean shouldUseContextGetDrawableMethod() {
return getEnvironment().getAndroidManifest().getMinSdkVersion() >= MIN_SDK_WITH_CONTEXT_GET_DRAWABLE;
}

private boolean hasGetDrawableInContext() {
TypeElement context = getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT);

return hasGetDrawable(context);
}

private void createCallWithIfGuard(EComponentHolder holder, JFieldRef ref, JBlock methodBody, JFieldRef idRef) {
JVar resourcesRef = holder.getResourcesRef();
JConditional guardIf = methodBody._if(getClasses().BUILD_VERSION.staticRef("SDK_INT").gte(getClasses().BUILD_VERSION_CODES.staticRef("LOLLIPOP")));
JBlock ifBlock = guardIf._then();
ifBlock.assign(ref, holder.getContextRef().invoke("getDrawable").arg(idRef));

JBlock elseBlock = guardIf._else();
elseBlock.assign(ref, resourcesRef.invoke("getDrawable").arg(idRef));
}

private boolean hasGetDrawable(TypeElement type) {
if (type == null) {
return false;
}

List<? extends Element> allMembers = getProcessingEnvironment().getElementUtils().getAllMembers(type);
for (ExecutableElement element : ElementFilter.methodsIn(allMembers)) {
if (element.getSimpleName().contentEquals("getDrawable")) {
return true;
}
}
return false;
super(AndroidRes.DRAWABLE, environment, MIN_SDK_WITH_CONTEXT_GET_DRAWABLE, MIN_SDK_PLATFORM_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2010-2015 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.generation;


import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.res.ColorRes;

import android.app.Activity;

@EActivity
public class ActivityWithColor extends Activity {
@ColorRes(R.color.myColor)
int myColor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2010-2015 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.generation;


import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.res.ColorStateListRes;

import android.app.Activity;
import android.content.res.ColorStateList;

@EActivity
public class ActivityWithColorStateList extends Activity {
@ColorStateListRes(R.color.myColorStateList)
ColorStateList myColorStateList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (C) 2010-2015 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.generation;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.res.ColorRes;

import android.app.Activity;

@EActivity
public class ActivityWithGetColorMethod extends Activity {

@ColorRes(R.color.myColor)
int myColor;

// http://developer.android.com/reference/android/content/Context.html#getColor(int)
// This method was added in API 23 and should be used to get a color which is styled/adjusted for the current theme.
public int getColor(int id) {
return 0;
}

}
Loading