Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 @@ -22,6 +22,7 @@
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;

import org.androidannotations.annotations.res.DrawableRes;
import org.androidannotations.annotations.res.HtmlRes;
import org.androidannotations.helper.AndroidManifest;
import org.androidannotations.helper.CanonicalNameConstants;
Expand Down Expand Up @@ -88,9 +89,15 @@ public void process(Element element, EComponentHolder holder) {
// Special case for @HtmlRes
if (element.getAnnotation(HtmlRes.class) != null) {
methodBody.assign(ref(fieldName), classes().HTML.staticInvoke("fromHtml").arg(invoke(holder.getResourcesRef(), resourceMethodName).arg(idRef)));
} else if (element.getAnnotation(DrawableRes.class) != null && hasContextCompatInClasspath()) {
methodBody.assign(ref(fieldName), classes().CONTEXT_COMPAT.staticInvoke("getDrawable").arg(holder.getContextRef()).arg(idRef));
} else {
methodBody.assign(ref(fieldName), invoke(holder.getResourcesRef(), resourceMethodName).arg(idRef));
}
}
}

protected boolean hasContextCompatInClasspath() {
return processingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.CONTEXT_COMPAT) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public final class CanonicalNameConstants {
public static final String SUPPORT_V4_PREFERENCE_FRAGMENT = "android.support.v4.preference.PreferenceFragment";
public static final String MACHINARIUS_V4_PREFERENCE_FRAGMENT = "com.github.machinarius.preferencefragment.PreferenceFragment";
public static final String ACTIVITY_COMPAT = "android.support.v4.app.ActivityCompat";
public static final String CONTEXT_COMPAT = "android.support.v4.content.ContextCompat";
public static final String PREFERENCE = "android.preference.Preference";
public static final String PREFERENCE_CHANGE_LISTENER = "android.preference.Preference.OnPreferenceChangeListener";
public static final String PREFERENCE_CLICK_LISTENER = "android.preference.Preference.OnPreferenceClickListener";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class Classes {
public final JClass BUILD_VERSION = refClass(CanonicalNameConstants.BUILD_VERSION);
public final JClass BUILD_VERSION_CODES = refClass(CanonicalNameConstants.BUILD_VERSION_CODES);
public final JClass ACTIVITY_COMPAT = refClass(CanonicalNameConstants.ACTIVITY_COMPAT);
public final JClass CONTEXT_COMPAT = refClass(CanonicalNameConstants.CONTEXT_COMPAT);
public final JClass APP_WIDGET_MANAGER = refClass(CanonicalNameConstants.APP_WIDGET_MANAGER);

public final JClass PREFERENCE = refClass(CanonicalNameConstants.PREFERENCE);
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.generation;


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

import android.app.Activity;
import android.graphics.drawable.Drawable;

@EActivity
public class ActivityWithDrawable extends Activity {
@DrawableRes(R.drawable.myDrawable)
Drawable myDrawable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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 java.io.File;

import org.androidannotations.AndroidAnnotationProcessor;
import org.androidannotations.utils.AAProcessorTestHelper;
import org.junit.Before;
import org.junit.Test;

public class ContextCompatDrawableTest extends AAProcessorTestHelper {

private static final String DRAWABLE_SIGNATURE = ".*myDrawable = resources_\\.getDrawable\\(drawable\\.myDrawable\\);.*";
private static final String DRAWABLE_VIA_SUPPORT_SIGNATURE = ".*myDrawable = ContextCompat\\.getDrawable\\(this, drawable\\.myDrawable\\);.*";

@Before
public void setUp() {
addProcessor(AndroidAnnotationProcessor.class);
addManifestProcessorParameter(ContextCompatDrawableTest.class, "AndroidManifestForDrawable.xml");
}

@Test
public void activityCompilesWithRegularDrawable() {
CompileResult result = compileFiles(ActivityWithDrawable.class);
File generatedFile = toGeneratedFile(ActivityWithDrawable.class);

assertCompilationSuccessful(result);
assertGeneratedClassMatches(generatedFile, DRAWABLE_SIGNATURE);
}

@Test
public void activityCompilesWithContextCompatDrawable() {
// To simulate android support v4 in classpath, we add
// android.support.v4.content.ContextCompat
// in classpath
CompileResult result = compileFiles(toPath(ContextCompatDrawableTest.class, "ContextCompat.java"), ActivityWithDrawable.class);
File generatedFile = toGeneratedFile(ActivityWithDrawable.class);

assertCompilationSuccessful(result);
assertGeneratedClassMatches(generatedFile, DRAWABLE_VIA_SUPPORT_SIGNATURE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.androidannotations.generation;

public class R {
public static final class drawable {
public static final int myDrawable = 0x7f06000a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--

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.

-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.androidannotations.generation"
android:versionCode="1"
android:versionName="1.0" >

<application>
<activity android:name="org.androidannotations.generation.ActivityWithDrawable_" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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 android.support.v4.content;

import android.content.Context;
import android.graphics.drawable.Drawable;

public class ContextCompat {

public static Drawable getDrawable(Context context, int id) {
return null;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.