This repository was archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix/1422 for corner cases. #1427
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
.../androidannotations/src/main/java/org/androidannotations/handler/AnimationResHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * 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.handler; | ||
|
|
||
| import static com.sun.codemodel.JExpr.ref; | ||
|
|
||
| import javax.annotation.processing.ProcessingEnvironment; | ||
|
|
||
| import org.androidannotations.holder.EComponentHolder; | ||
| import org.androidannotations.model.AndroidRes; | ||
|
|
||
| import com.sun.codemodel.JBlock; | ||
| import com.sun.codemodel.JFieldRef; | ||
|
|
||
| public class AnimationResHandler extends AbstractResHandler { | ||
|
|
||
| public AnimationResHandler(ProcessingEnvironment processingEnvironment) { | ||
| super(AndroidRes.ANIMATION, processingEnvironment); | ||
| } | ||
|
|
||
| @Override | ||
| protected void makeCall(String fieldName, EComponentHolder holder, JBlock methodBody, JFieldRef idRef) { | ||
| methodBody.assign(ref(fieldName), classes().ANIMATION_UTILS.staticInvoke("loadAnimation").arg(holder.getContextRef()).arg(idRef)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...ns/androidannotations/src/main/java/org/androidannotations/handler/DefaultResHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * 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.handler; | ||
|
|
||
| import static com.sun.codemodel.JExpr.invoke; | ||
| import static com.sun.codemodel.JExpr.ref; | ||
|
|
||
| import javax.annotation.processing.ProcessingEnvironment; | ||
|
|
||
| import org.androidannotations.holder.EComponentHolder; | ||
| import org.androidannotations.model.AndroidRes; | ||
|
|
||
| import com.sun.codemodel.JBlock; | ||
| import com.sun.codemodel.JFieldRef; | ||
|
|
||
| public class DefaultResHandler extends AbstractResHandler { | ||
|
|
||
| public DefaultResHandler(AndroidRes androidRes, ProcessingEnvironment processingEnvironment) { | ||
| super(androidRes, processingEnvironment); | ||
| } | ||
|
|
||
| @Override | ||
| protected void makeCall(String fieldName, EComponentHolder holder, JBlock methodBody, JFieldRef idRef) { | ||
| methodBody.assign(ref(fieldName), invoke(holder.getResourcesRef(), androidRes.getResourceMethodName()).arg(idRef)); | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
...s/androidannotations/src/main/java/org/androidannotations/handler/DrawableResHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * 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.AnimationResHandler.java | ||
| */ | ||
| package org.androidannotations.handler; | ||
|
|
||
| import static com.sun.codemodel.JExpr.invoke; | ||
| import static com.sun.codemodel.JExpr.ref; | ||
|
|
||
| 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.TypeElement; | ||
| import javax.lang.model.util.ElementFilter; | ||
|
|
||
| import org.androidannotations.helper.CanonicalNameConstants; | ||
| import org.androidannotations.holder.EComponentHolder; | ||
| import org.androidannotations.model.AndroidRes; | ||
|
|
||
| import com.sun.codemodel.JBlock; | ||
| import com.sun.codemodel.JConditional; | ||
| import com.sun.codemodel.JFieldRef; | ||
| import com.sun.codemodel.JVar; | ||
|
|
||
| public class DrawableResHandler extends AbstractResHandler { | ||
|
|
||
| private static final int MIN_SDK_WITH_CONTEXT_GET_DRAWABLE = 21; | ||
|
|
||
| public DrawableResHandler(ProcessingEnvironment processingEnvironment) { | ||
| super(AndroidRes.DRAWABLE, processingEnvironment); | ||
| } | ||
|
|
||
| @Override | ||
| protected void makeCall(String fieldName, EComponentHolder holder, JBlock methodBody, JFieldRef idRef) { | ||
| JFieldRef ref = ref(fieldName); | ||
| if (hasContextCompatInClasspath()) { | ||
| methodBody.assign(ref, classes().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)); | ||
| } | ||
| } | ||
|
|
||
| protected boolean hasContextCompatInClasspath() { | ||
| return processingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT_COMPAT) != null; | ||
| } | ||
|
|
||
| protected boolean shouldUseContextGetDrawableMethod() { | ||
| return androidManifest.getMinSdkVersion() >= MIN_SDK_WITH_CONTEXT_GET_DRAWABLE; | ||
| } | ||
|
|
||
| protected boolean hasGetDrawableInContext() { | ||
| TypeElement context = processingEnvironment().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(holder.classes().BUILD_VERSION.staticRef("SDK_INT").gte(holder.classes().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 = processingEnvironment().getElementUtils().getAllMembers(type); | ||
| for (ExecutableElement element : ElementFilter.methodsIn(allMembers)) { | ||
| if (element.getSimpleName().contentEquals("getDrawable")) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...tions/androidannotations/src/main/java/org/androidannotations/handler/HtmlResHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * 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.handler; | ||
|
|
||
| import static com.sun.codemodel.JExpr.ref; | ||
|
|
||
| import javax.annotation.processing.ProcessingEnvironment; | ||
|
|
||
| import org.androidannotations.holder.EComponentHolder; | ||
| import org.androidannotations.model.AndroidRes; | ||
|
|
||
| import com.sun.codemodel.JBlock; | ||
| import com.sun.codemodel.JFieldRef; | ||
|
|
||
| public class HtmlResHandler extends AbstractResHandler { | ||
|
|
||
| public HtmlResHandler(ProcessingEnvironment processingEnvironment) { | ||
| super(AndroidRes.HTML, processingEnvironment); | ||
| } | ||
|
|
||
| @Override | ||
| protected void makeCall(String fieldName, EComponentHolder holder, JBlock methodBody, JFieldRef idRef) { | ||
| methodBody.assign(ref(fieldName), classes().HTML.staticInvoke("fromHtml").arg(holder.getResourcesRef().invoke(AndroidRes.HTML.getResourceMethodName()).arg(idRef))); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
...ations/src/test/java/org/androidannotations/generation/ActivityWithGetDrawableMethod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * 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.DrawableRes; | ||
|
|
||
| import android.app.Activity; | ||
| import android.graphics.drawable.Drawable; | ||
|
|
||
| @EActivity | ||
| public class ActivityWithGetDrawableMethod extends Activity { | ||
|
|
||
| @DrawableRes(R.drawable.myDrawable) | ||
| Drawable myDrawable; | ||
|
|
||
| // http://developer.android.com/reference/android/content/Context.html#getDrawable(int) | ||
| // This method was added in API 21 and should be used to get a Drawable which is styled/adjusted for the current theme. | ||
| public Drawable getDrawable(int drawableId) { | ||
| return null; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add a comment to clarify the origin of this method.