use of android.text.style.StyleSpan in project plaid by nickbutcher.
the class Bypass method recurseElement.
// The 'numberOfSiblings' parameters refers to the number of siblings within the parent, including
// the 'element' parameter, as in "How many siblings are you?" rather than "How many siblings do
// you have?".
private CharSequence recurseElement(Element element, int indexWithinParent, int numberOfSiblings, TextView textView, LoadImageCallback loadImageCallback) {
Type type = element.getType();
boolean isOrderedList = false;
if (type == Type.LIST) {
String flagsStr = element.getAttribute("flags");
if (flagsStr != null) {
int flags = Integer.parseInt(flagsStr);
isOrderedList = (flags & Element.F_LIST_ORDERED) != 0;
if (isOrderedList) {
mOrderedListNumber.put(element, 1);
}
}
}
int size = element.size();
CharSequence[] spans = new CharSequence[size];
for (int i = 0; i < size; i++) {
spans[i] = recurseElement(element.children[i], i, size, textView, loadImageCallback);
}
// Clean up after we're done
if (isOrderedList) {
mOrderedListNumber.remove(this);
}
CharSequence concat = TextUtils.concat(spans);
SpannableStringBuilder builder = new ReverseSpannableStringBuilder();
String text = element.getText();
if (element.size() == 0 && element.getParent() != null && element.getParent().getType() != Type.BLOCK_CODE) {
text = text.replace('\n', ' ');
}
switch(type) {
case LIST:
if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
builder.append("\n");
}
break;
case LINEBREAK:
builder.append("\n");
break;
case LIST_ITEM:
builder.append(" ");
if (mOrderedListNumber.containsKey(element.getParent())) {
int number = mOrderedListNumber.get(element.getParent());
builder.append(Integer.toString(number) + ".");
mOrderedListNumber.put(element.getParent(), number + 1);
} else {
builder.append(mOptions.mUnorderedListItem);
}
builder.append(" ");
break;
case AUTOLINK:
builder.append(element.getAttribute("link"));
break;
case HRULE:
// This ultimately gets drawn over by the line span, but
// we need something here or the span isn't even drawn.
builder.append("-");
break;
case IMAGE:
if (loadImageCallback != null && !TextUtils.isEmpty(element.getAttribute("link"))) {
// prepend a new line so that images are always on a new line
builder.append("\n");
// Display alt text (or title text) if there is no image
String alt = element.getAttribute("alt");
if (TextUtils.isEmpty(alt)) {
alt = element.getAttribute("title");
}
if (!TextUtils.isEmpty(alt)) {
alt = "[" + alt + "]";
builder.append(alt);
} else {
// Character to be replaced
builder.append("");
}
} else {
// Character to be replaced
builder.append("");
}
break;
}
builder.append(text);
builder.append(concat);
// of the last child within the parent.
if (element.getParent() != null || indexWithinParent < (numberOfSiblings - 1)) {
if (type == Type.LIST_ITEM) {
if (element.size() == 0 || !element.children[element.size() - 1].isBlockElement()) {
builder.append("\n");
}
} else if (element.isBlockElement() && type != Type.BLOCK_QUOTE) {
if (type == Type.LIST) {
// If this is a nested list, don't include newlines
if (element.getParent() == null || element.getParent().getType() != Type.LIST_ITEM) {
builder.append("\n");
}
} else if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
// List items should never double-space their entries
builder.append("\n");
} else {
builder.append("\n\n");
}
}
}
switch(type) {
case HEADER:
String levelStr = element.getAttribute("level");
int level = Integer.parseInt(levelStr);
setSpan(builder, new RelativeSizeSpan(mOptions.mHeaderSizes[level - 1]));
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case LIST:
setBlockSpan(builder, new LeadingMarginSpan.Standard(mListItemIndent));
break;
case EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case DOUBLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case TRIPLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD_ITALIC));
break;
case BLOCK_CODE:
setSpan(builder, new LeadingMarginSpan.Standard(mCodeBlockIndent));
setSpan(builder, new TypefaceSpan("monospace"));
break;
case CODE_SPAN:
setSpan(builder, new TypefaceSpan("monospace"));
break;
case LINK:
case AUTOLINK:
String link = element.getAttribute("link");
if (!TextUtils.isEmpty(link) && Patterns.EMAIL_ADDRESS.matcher(link).matches()) {
link = "mailto:" + link;
}
setSpan(builder, new TouchableUrlSpan(link, textView.getLinkTextColors(), textView.getHighlightColor()));
break;
case BLOCK_QUOTE:
// We add two leading margin spans so that when the order is reversed,
// the QuoteSpan will always be in the same spot.
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
//setBlockSpan(builder, new QuoteSpan(mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new FancyQuoteSpan(mBlockQuoteLineWidth, mBlockQuoteLineIndent, mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new ForegroundColorSpan(mOptions.mBlockQuoteTextColor));
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
setBlockSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case STRIKETHROUGH:
setSpan(builder, new StrikethroughSpan());
break;
case HRULE:
setSpan(builder, new HorizontalLineSpan(mOptions.mHruleColor, mHruleSize, mHruleTopBottomPadding));
break;
case IMAGE:
String url = element.getAttribute("link");
if (loadImageCallback != null && !TextUtils.isEmpty(url)) {
setPrependedNewlineSpan(builder, mOptions.mPreImageLinebreakHeight);
ImageLoadingSpan loadingSpan = new ImageLoadingSpan();
setSpanWithPrependedNewline(builder, loadingSpan);
// make the (eventually loaded) image span clickable to open in browser
setSpanWithPrependedNewline(builder, new TouchableUrlSpan(url, textView.getLinkTextColors(), textView.getHighlightColor()));
loadImageCallback.loadImage(url, loadingSpan);
}
break;
}
return builder;
}
use of android.text.style.StyleSpan in project android_frameworks_base by ParanoidAndroid.
the class ProgressDialog method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(mContext);
TypedArray a = mContext.obtainStyledAttributes(null, com.android.internal.R.styleable.AlertDialog, com.android.internal.R.attr.alertDialogStyle, 0);
if (mProgressStyle == STYLE_HORIZONTAL) {
/* Use a separate handler to update the text views as they
* must be updated on the same thread that created them.
*/
mViewUpdateHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
/* Update the number and percent */
int progress = mProgress.getProgress();
int max = mProgress.getMax();
if (mProgressNumberFormat != null) {
String format = mProgressNumberFormat;
mProgressNumber.setText(String.format(format, progress, max));
} else {
mProgressNumber.setText("");
}
if (mProgressPercentFormat != null) {
double percent = (double) progress / (double) max;
SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mProgressPercent.setText(tmp);
} else {
mProgressPercent.setText("");
}
}
};
View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout, R.layout.alert_dialog_progress), null);
mProgress = (ProgressBar) view.findViewById(R.id.progress);
mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
setView(view);
} else {
View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_progressLayout, R.layout.progress_dialog), null);
mProgress = (ProgressBar) view.findViewById(R.id.progress);
mMessageView = (TextView) view.findViewById(R.id.message);
setView(view);
}
a.recycle();
if (mMax > 0) {
setMax(mMax);
}
if (mProgressVal > 0) {
setProgress(mProgressVal);
}
if (mSecondaryProgressVal > 0) {
setSecondaryProgress(mSecondaryProgressVal);
}
if (mIncrementBy > 0) {
incrementProgressBy(mIncrementBy);
}
if (mIncrementSecondaryBy > 0) {
incrementSecondaryProgressBy(mIncrementSecondaryBy);
}
if (mProgressDrawable != null) {
setProgressDrawable(mProgressDrawable);
}
if (mIndeterminateDrawable != null) {
setIndeterminateDrawable(mIndeterminateDrawable);
}
if (mMessage != null) {
setMessage(mMessage);
}
setIndeterminate(mIndeterminate);
onProgressChanged();
super.onCreate(savedInstanceState);
}
use of android.text.style.StyleSpan in project android_frameworks_base by ParanoidAndroid.
the class HtmlTest method testMarkup.
@SmallTest
public void testMarkup() throws Exception {
SpannableString s;
s = new SpannableString("Hello bold world");
s.setSpan(new StyleSpan(Typeface.BOLD), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <b>bold</b> world</p>\n");
s = new SpannableString("Hello italic world");
s.setSpan(new StyleSpan(Typeface.ITALIC), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <i>italic</i> world</p>\n");
s = new SpannableString("Hello monospace world");
s.setSpan(new TypefaceSpan("monospace"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <tt>monospace</tt> world</p>\n");
s = new SpannableString("Hello superscript world");
s.setSpan(new SuperscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <sup>superscript</sup> world</p>\n");
s = new SpannableString("Hello subscript world");
s.setSpan(new SubscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <sub>subscript</sub> world</p>\n");
s = new SpannableString("Hello underline world");
s.setSpan(new UnderlineSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <u>underline</u> world</p>\n");
s = new SpannableString("Hello struck world");
s.setSpan(new StrikethroughSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <strike>struck</strike> world</p>\n");
s = new SpannableString("Hello linky world");
s.setSpan(new URLSpan("http://www.google.com"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
assertEquals(Html.toHtml(s), "<p>Hello <a href=\"http://www.google.com\">linky</a> world</p>\n");
}
use of android.text.style.StyleSpan in project android_frameworks_base by ParanoidAndroid.
the class TextUtilsTest method testEllipsize.
@LargeTest
public void testEllipsize() {
CharSequence s1 = "The quick brown fox jumps over þhe lazy dog.";
CharSequence s2 = new Wrapper(s1);
Spannable s3 = new SpannableString(s1);
s3.setSpan(new StyleSpan(0), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextPaint p = new TextPaint();
p.setFlags(p.getFlags() & ~p.DEV_KERN_TEXT_FLAG);
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 3; j++) {
TextUtils.TruncateAt kind = null;
switch(j) {
case 0:
kind = TextUtils.TruncateAt.START;
break;
case 1:
kind = TextUtils.TruncateAt.END;
break;
case 2:
kind = TextUtils.TruncateAt.MIDDLE;
break;
}
String out1 = TextUtils.ellipsize(s1, p, i, kind).toString();
String out2 = TextUtils.ellipsize(s2, p, i, kind).toString();
String out3 = TextUtils.ellipsize(s3, p, i, kind).toString();
String keep1 = TextUtils.ellipsize(s1, p, i, kind, true, null).toString();
String keep2 = TextUtils.ellipsize(s2, p, i, kind, true, null).toString();
String keep3 = TextUtils.ellipsize(s3, p, i, kind, true, null).toString();
String trim1 = keep1.replace("", "");
// Are all normal output strings identical?
assertEquals("wid " + i + " pass " + j, out1, out2);
assertEquals("wid " + i + " pass " + j, out2, out3);
// Are preserved output strings identical?
assertEquals("wid " + i + " pass " + j, keep1, keep2);
assertEquals("wid " + i + " pass " + j, keep2, keep3);
// Does trimming padding from preserved yield normal?
assertEquals("wid " + i + " pass " + j, out1, trim1);
// Did preserved output strings preserve length?
assertEquals("wid " + i + " pass " + j, keep1.length(), s1.length());
// Does the output string actually fit in the space?
assertTrue("wid " + i + " pass " + j, p.measureText(out1) <= i);
// Is the padded output the same width as trimmed output?
assertTrue("wid " + i + " pass " + j, p.measureText(keep1) == p.measureText(out1));
}
}
}
use of android.text.style.StyleSpan in project MPAndroidChart by PhilJay.
the class HalfPieChartActivity method generateCenterSpannableText.
private SpannableString generateCenterSpannableText() {
SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
return s;
}
Aggregations