This repository has been 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.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fromhtml' into develop
- Loading branch information
Showing
10 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
12 changes: 12 additions & 0 deletions
12
...roidannotations/src/main/java/com/googlecode/androidannotations/annotations/FromHtml.java
This file contains 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,12 @@ | ||
package com.googlecode.androidannotations.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.SOURCE) | ||
@Target(ElementType.FIELD) | ||
public @interface FromHtml { | ||
int value() default Id.DEFAULT_VALUE; | ||
} |
This file contains 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
51 changes: 51 additions & 0 deletions
51
...tations/src/main/java/com/googlecode/androidannotations/processing/FromHtmlProcessor.java
This file contains 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,51 @@ | ||
package com.googlecode.androidannotations.processing; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import javax.lang.model.element.Element; | ||
|
||
import com.googlecode.androidannotations.annotations.FromHtml; | ||
import com.googlecode.androidannotations.annotations.Id; | ||
import com.googlecode.androidannotations.rclass.IRClass; | ||
import com.googlecode.androidannotations.rclass.IRClass.Res; | ||
import com.googlecode.androidannotations.rclass.IRInnerClass; | ||
import com.sun.codemodel.JBlock; | ||
import com.sun.codemodel.JCodeModel; | ||
import com.sun.codemodel.JExpr; | ||
import com.sun.codemodel.JFieldRef; | ||
|
||
public class FromHtmlProcessor implements ElementProcessor { | ||
|
||
private final IRClass rClass; | ||
|
||
public FromHtmlProcessor(IRClass rClass) { | ||
this.rClass = rClass; | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> getTarget() { | ||
return FromHtml.class; | ||
} | ||
|
||
@Override | ||
public void process(Element element, JCodeModel codeModel, ActivitiesHolder activitiesHolder) throws Exception { | ||
ActivityHolder holder = activitiesHolder.getEnclosingActivityHolder(element); | ||
|
||
String fieldName = element.getSimpleName().toString(); | ||
|
||
FromHtml annotation = element.getAnnotation(FromHtml.class); | ||
int idValue = annotation.value(); | ||
|
||
IRInnerClass rInnerClass = rClass.get(Res.STRING); | ||
JFieldRef idRef; | ||
if (idValue == Id.DEFAULT_VALUE) { | ||
idRef = rInnerClass.getIdStaticRef(fieldName, holder); | ||
} else { | ||
idRef = rInnerClass.getIdStaticRef(idValue, holder); | ||
} | ||
|
||
JBlock methodBody = holder.afterSetContentView.body(); | ||
|
||
methodBody._if(JExpr.ref(fieldName).ne(JExpr._null()))._then().invoke(JExpr.ref(fieldName), "setText").arg(holder.refClass("android.text.Html").staticInvoke("fromHtml").arg(JExpr.invoke("getString").arg(idRef))); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...tations/src/main/java/com/googlecode/androidannotations/validation/FromHtmlValidator.java
This file contains 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,43 @@ | ||
package com.googlecode.androidannotations.validation; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import javax.annotation.processing.ProcessingEnvironment; | ||
import javax.lang.model.element.Element; | ||
|
||
import com.googlecode.androidannotations.annotations.FromHtml; | ||
import com.googlecode.androidannotations.helper.IdAnnotationHelper; | ||
import com.googlecode.androidannotations.helper.IdValidatorHelper; | ||
import com.googlecode.androidannotations.model.AnnotationElements; | ||
import com.googlecode.androidannotations.rclass.IRClass; | ||
import com.googlecode.androidannotations.rclass.IRClass.Res; | ||
|
||
public class FromHtmlValidator implements ElementValidator { | ||
|
||
private IdValidatorHelper validatorHelper; | ||
|
||
public FromHtmlValidator(ProcessingEnvironment processingEnv, IRClass rClass) { | ||
IdAnnotationHelper annotationHelper = new IdAnnotationHelper(processingEnv, getTarget(), rClass); | ||
validatorHelper = new IdValidatorHelper(annotationHelper); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> getTarget() { | ||
return FromHtml.class; | ||
} | ||
|
||
@Override | ||
public boolean validate(Element element, AnnotationElements validatedElements) { | ||
|
||
IsValid valid = new IsValid(); | ||
|
||
validatorHelper.hasViewByIdAnnotation(element, validatedElements, valid); | ||
|
||
validatorHelper.extendsTextView(element, valid); | ||
|
||
validatorHelper.idExists(element, Res.STRING, valid); | ||
|
||
return valid.isValid(); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
20 changes: 20 additions & 0 deletions
20
...nal-test-1-5/src/main/java/com/googlecode/androidannotations/test15/FromHtmlActivity.java
This file contains 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,20 @@ | ||
package com.googlecode.androidannotations.test15; | ||
|
||
import android.app.Activity; | ||
import android.widget.TextView; | ||
|
||
import com.googlecode.androidannotations.annotations.EActivity; | ||
import com.googlecode.androidannotations.annotations.FromHtml; | ||
import com.googlecode.androidannotations.annotations.ViewById; | ||
|
||
@EActivity(R.layout.views_injected) | ||
public class FromHtmlActivity extends Activity { | ||
|
||
@ViewById(R.id.my_text_view) | ||
@FromHtml(R.string.hello_html) | ||
TextView textView; | ||
|
||
@ViewById | ||
@FromHtml | ||
TextView someView; | ||
} |
40 changes: 40 additions & 0 deletions
40
...test-1-5/src/test/java/com/googlecode/androidannotations/test15/FromHtmlActivityTest.java
This file contains 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,40 @@ | ||
package com.googlecode.androidannotations.test15; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import android.text.Html; | ||
|
||
import com.xtremelabs.robolectric.Robolectric; | ||
import com.xtremelabs.robolectric.RobolectricTestRunner; | ||
import com.xtremelabs.robolectric.shadows.ShadowHtml; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class FromHtmlActivityTest { | ||
|
||
private FromHtmlActivity_ activity; | ||
|
||
@Before | ||
public void setup() { | ||
Robolectric.bindShadowClass(ShadowHtml.class); | ||
|
||
activity = new FromHtmlActivity_(); | ||
activity.onCreate(null); | ||
} | ||
|
||
@Test | ||
public void injectionOfHtmlTest() { | ||
assertNotNull(activity.textView); | ||
assertEquals(Html.fromHtml(activity.getString(R.string.hello_html)), activity.textView.getText()); | ||
} | ||
|
||
@Test | ||
public void injectionOfHtmlWithDefaultName() { | ||
assertNotNull(activity.someView); | ||
assertEquals(Html.fromHtml(activity.getString(R.string.someView)), activity.someView.getText()); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...ions/functional-test-1-5/src/test/java/com/xtremelabs/robolectric/shadows/ShadowHtml.java
This file contains 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,81 @@ | ||
package com.xtremelabs.robolectric.shadows; | ||
|
||
import android.text.Html; | ||
import android.text.Spanned; | ||
|
||
import com.xtremelabs.robolectric.internal.Implementation; | ||
import com.xtremelabs.robolectric.internal.Implements; | ||
|
||
@Implements(Html.class) | ||
public class ShadowHtml { | ||
@Implementation | ||
public static Spanned fromHtml(String source) { | ||
return new SpannedThatActsLikeString(source); | ||
} | ||
|
||
private static class SpannedThatActsLikeString implements Spanned { | ||
String source; | ||
|
||
private SpannedThatActsLikeString(String source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public <T> T[] getSpans(int start, int end, Class<T> type) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getSpanStart(Object tag) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getSpanEnd(Object tag) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getSpanFlags(Object tag) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int nextSpanTransition(int start, int limit, Class type) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int length() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public char charAt(int i) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public CharSequence subSequence(int i, int i1) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return source; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o instanceof SpannedThatActsLikeString) | ||
return source.equals(((SpannedThatActsLikeString) o).source); | ||
else | ||
return source.equals(o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return source != null ? source.hashCode() : 0; | ||
} | ||
} | ||
} |