Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit e2b1fde

Browse files
committed
If ContextCompat is available, use it to resolve drawables
1 parent 92d2f8a commit e2b1fde

8 files changed

Lines changed: 156 additions & 0 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ResHandler.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.lang.model.element.Element;
2323
import javax.lang.model.type.TypeMirror;
2424

25+
import org.androidannotations.annotations.res.DrawableRes;
2526
import org.androidannotations.annotations.res.HtmlRes;
2627
import org.androidannotations.helper.AndroidManifest;
2728
import org.androidannotations.helper.CanonicalNameConstants;
@@ -88,9 +89,15 @@ public void process(Element element, EComponentHolder holder) {
8889
// Special case for @HtmlRes
8990
if (element.getAnnotation(HtmlRes.class) != null) {
9091
methodBody.assign(ref(fieldName), classes().HTML.staticInvoke("fromHtml").arg(invoke(holder.getResourcesRef(), resourceMethodName).arg(idRef)));
92+
} else if (element.getAnnotation(DrawableRes.class) != null && hasContextCompatInClasspath()) {
93+
methodBody.assign(ref(fieldName), classes().CONTEXT_COMPAT.staticInvoke("getDrawable").arg(holder.getContextRef()).arg(idRef));
9194
} else {
9295
methodBody.assign(ref(fieldName), invoke(holder.getResourcesRef(), resourceMethodName).arg(idRef));
9396
}
9497
}
9598
}
99+
100+
protected boolean hasContextCompatInClasspath() {
101+
return processingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT_COMPAT) != null;
102+
}
96103
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public final class CanonicalNameConstants {
116116
public static final String SUPPORT_V4_PREFERENCE_FRAGMENT = "android.support.v4.preference.PreferenceFragment";
117117
public static final String MACHINARIUS_V4_PREFERENCE_FRAGMENT = "com.github.machinarius.preferencefragment.PreferenceFragment";
118118
public static final String ACTIVITY_COMPAT = "android.support.v4.app.ActivityCompat";
119+
public static final String CONTEXT_COMPAT = "android.support.v4.content.ContextCompat";
119120
public static final String PREFERENCE = "android.preference.Preference";
120121
public static final String PREFERENCE_CHANGE_LISTENER = "android.preference.Preference.OnPreferenceChangeListener";
121122
public static final String PREFERENCE_CLICK_LISTENER = "android.preference.Preference.OnPreferenceClickListener";

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/process/ProcessHolder.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public class Classes {
122122
public final JClass BUILD_VERSION = refClass(CanonicalNameConstants.BUILD_VERSION);
123123
public final JClass BUILD_VERSION_CODES = refClass(CanonicalNameConstants.BUILD_VERSION_CODES);
124124
public final JClass ACTIVITY_COMPAT = refClass(CanonicalNameConstants.ACTIVITY_COMPAT);
125+
public final JClass CONTEXT_COMPAT = refClass(CanonicalNameConstants.CONTEXT_COMPAT);
125126
public final JClass APP_WIDGET_MANAGER = refClass(CanonicalNameConstants.APP_WIDGET_MANAGER);
126127

127128
public final JClass PREFERENCE = refClass(CanonicalNameConstants.PREFERENCE);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 org.androidannotations.generation;
17+
18+
19+
import org.androidannotations.annotations.EActivity;
20+
import org.androidannotations.annotations.res.DrawableRes;
21+
22+
import android.app.Activity;
23+
import android.graphics.drawable.Drawable;
24+
25+
@EActivity
26+
public class ActivityWithDrawable extends Activity {
27+
@DrawableRes(R.drawable.myDrawable)
28+
Drawable myDrawable;
29+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 org.androidannotations.generation;
17+
18+
import java.io.File;
19+
20+
import org.androidannotations.AndroidAnnotationProcessor;
21+
import org.androidannotations.utils.AAProcessorTestHelper;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
public class ContextCompatDrawableTest extends AAProcessorTestHelper {
26+
27+
private static final String DRAWABLE_SIGNATURE = ".*myDrawable = resources_\\.getDrawable\\(drawable\\.myDrawable\\);.*";
28+
private static final String DRAWABLE_VIA_SUPPORT_SIGNATURE = ".*myDrawable = ContextCompat\\.getDrawable\\(this, drawable\\.myDrawable\\);.*";
29+
30+
@Before
31+
public void setUp() {
32+
addProcessor(AndroidAnnotationProcessor.class);
33+
}
34+
35+
@Test
36+
public void activityCompilesWithRegularDrawable() {
37+
addManifestProcessorParameter(ContextCompatDrawableTest.class, "AndroidManifestForDrawable.xml");
38+
CompileResult result = compileFiles(ActivityWithDrawable.class);
39+
File generatedFile = toGeneratedFile(ActivityWithDrawable.class);
40+
41+
assertCompilationSuccessful(result);
42+
assertGeneratedClassMatches(generatedFile, DRAWABLE_SIGNATURE);
43+
}
44+
45+
@Test
46+
public void activityCompilesWithContextCompatRegularDrawable() {
47+
// To simulate android support v4 in classpath, we add
48+
// android.support.v4.content.ContextCompat
49+
// in classpath
50+
addManifestProcessorParameter(ContextCompatDrawableTest.class, "AndroidManifestForDrawable.xml");
51+
CompileResult result = compileFiles(toPath(ContextCompatDrawableTest.class, "ContextCompat.java"), ActivityWithDrawable.class);
52+
File generatedFile = toGeneratedFile(ActivityWithDrawable.class);
53+
54+
assertCompilationSuccessful(result);
55+
assertGeneratedClassMatches(generatedFile, DRAWABLE_VIA_SUPPORT_SIGNATURE);
56+
}
57+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.androidannotations.generation;
2+
3+
public class R {
4+
public static final class drawable {
5+
public static final int myDrawable = 0x7f06000a;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2015 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
20+
package="org.androidannotations.generation"
21+
android:versionCode="1"
22+
android:versionName="1.0" >
23+
24+
<application>
25+
<activity android:name="org.androidannotations.generation.ActivityWithDrawable_" />
26+
</application>
27+
28+
</manifest>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
import android.content.Context;
19+
import android.graphics.drawable.Drawable;
20+
21+
public class ContextCompat {
22+
23+
public static Drawable getDrawable(Context context, int id) {
24+
return null;
25+
}
26+
}

0 commit comments

Comments
 (0)