Skip to content

Commit ccac75a

Browse files
committed
Do not use ContextCompat if target method is not available
1 parent ab53a8b commit ccac75a

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ContextCompatAwareResHandler.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ abstract class ContextCompatAwareResHandler extends AbstractResHandler {
4545

4646
@Override
4747
protected IJExpression getInstanceInvocation(EComponentHolder holder, JFieldRef idRef, IJAssignmentTarget fieldRef, JBlock targetBlock) {
48-
if (hasContextCompatInClasspath()) {
48+
if (hasTargetMethodInContextCompat()) {
4949
return getClasses().CONTEXT_COMPAT.staticInvoke(androidRes.getResourceMethodName()).arg(holder.getContextRef()).arg(idRef);
50-
} else if (shouldUseContextMethod() && !hasContextCompatInClasspath()) {
50+
} else if (shouldUseContextMethod() && !hasTargetMethodInContextCompat()) {
5151
return holder.getContextRef().invoke(androidRes.getResourceMethodName()).arg(idRef);
52-
} else if (!shouldUseContextMethod() && hasTargetMethodInContext() && !hasContextCompatInClasspath()) {
52+
} else if (!shouldUseContextMethod() && hasTargetMethodInContext() && !hasTargetMethodInContextCompat()) {
5353
return createCallWithIfGuard(holder, idRef, fieldRef, targetBlock);
5454
} else {
5555
return invoke(holder.getResourcesRef(), androidRes.getResourceMethodName()).arg(idRef);
@@ -66,6 +66,12 @@ private boolean hasTargetMethodInContext() {
6666
return hasTargetMethod(context, androidRes.getResourceMethodName());
6767
}
6868

69+
private boolean hasTargetMethodInContextCompat() {
70+
TypeElement contextCompat = getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT_COMPAT);
71+
72+
return hasTargetMethod(contextCompat, androidRes.getResourceMethodName());
73+
}
74+
6975
private IJExpression createCallWithIfGuard(EComponentHolder holder, JFieldRef idRef, IJAssignmentTarget fieldRef, JBlock targetBlock) {
7076
JVar resourcesRef = holder.getResourcesRef();
7177
IJExpression buildVersionCondition = getClasses().BUILD_VERSION.staticRef("SDK_INT").gte(getClasses().BUILD_VERSION_CODES.staticRef(minSdkPlatformName));
@@ -76,8 +82,4 @@ private IJExpression createCallWithIfGuard(EComponentHolder holder, JFieldRef id
7682

7783
return null;
7884
}
79-
80-
protected boolean hasContextCompatInClasspath() {
81-
return getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT_COMPAT) != null;
82-
}
8385
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/ContextCompatColorTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,16 @@ public void activityCompilesOnMinSdkLower23CompileSdkHigher22WithoutContextCompa
8686
assertGeneratedClassContains(generatedFile, COLOR_CONDITIONAL_WITHOUT_CONTEXT_COMPAT);
8787
}
8888

89+
@Test
90+
public void activityCompilesWithOldContextCompat() throws Exception {
91+
addManifestProcessorParameter(ContextCompatColorTest.class, "AndroidManifestForColorMinSdk23.xml");
92+
93+
CompileResult result = compileFiles(ActivityWithGetColorMethod.class,
94+
toPath(FragmentByChildFragmentManagerTest.class, "support/old/ContextCompat.java"));
95+
File generatedFile = toGeneratedFile(ActivityWithGetColorMethod.class);
96+
97+
assertCompilationSuccessful(result);
98+
assertGeneratedClassMatches(generatedFile, COLOR_VIA_CONTEXT_ON_MARSHMALLOW);
99+
}
100+
89101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package android.support.v4.content;
17+
18+
public class ContextCompat {
19+
20+
}

0 commit comments

Comments
 (0)