/* Copyright © 2013-2014, Silent Circle, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Any redistribution, use, or modification is done solely for personal benefit and not for any commercial purpose or for monetary gain * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name Silent Circle nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SILENT CIRCLE, LLC BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * This implementation is edited version of original Android sources. */ /* * Copyright (C) 2011 The Android Open Source Project * * 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 com.silentcircle.contacts.utils; import android.content.Context; import android.content.res.Resources; import android.text.Html; import android.text.Html.ImageGetter; import android.text.Html.TagHandler; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextUtils; import android.text.style.ImageSpan; import android.text.style.QuoteSpan; import com.silentcircle.contacts.R; import com.google.common.annotations.VisibleForTesting; /** * Provides static functions to perform custom HTML to text conversions. * Specifically, it adjusts the color and padding of the vertical * stripe on block quotes and alignment of inlined images. */ public class HtmlUtils { /** * Converts HTML string to a {@link Spanned} text, adjusting formatting. Any extra new line * characters at the end of the text will be trimmed. */ public static Spanned fromHtml(Context context, String text) { if (TextUtils.isEmpty(text)) { return null; } Spanned spanned = Html.fromHtml(text); return postprocess(context, spanned); } /** * Converts HTML string to a {@link Spanned} text, adjusting formatting and using a custom * image getter. Any extra new line characters at the end of the text will be trimmed. */ public static CharSequence fromHtml(Context context, String text, ImageGetter imageGetter, TagHandler tagHandler) { if (TextUtils.isEmpty(text)) { return null; } return postprocess(context, Html.fromHtml(text, imageGetter, tagHandler)); } /** * Replaces some spans with custom versions of those. Any extra new line characters at the end * of the text will be trimmed. */ @VisibleForTesting static Spanned postprocess(Context context, Spanned original) { if (original == null) { return null; } final int length = original.length(); if (length == 0) { return original; // Bail early. } // If it's a SpannableStringBuilder, just use it. Otherwise, create a new // SpannableStringBuilder based on the passed Spanned. final SpannableStringBuilder builder; if (original instanceof SpannableStringBuilder) { builder = (SpannableStringBuilder) original; } else { builder = new SpannableStringBuilder(original); } final QuoteSpan[] quoteSpans = builder.getSpans(0, length, QuoteSpan.class); if (quoteSpans != null && quoteSpans.length != 0) { Resources resources = context.getResources(); int color = resources.getColor(R.color.stream_item_stripe_color); int width = resources.getDimensionPixelSize(R.dimen.stream_item_stripe_width); for (int i = 0; i < quoteSpans.length; i++) { replaceSpan(builder, quoteSpans[i], new StreamItemQuoteSpan(color, width)); } } final ImageSpan[] imageSpans = builder.getSpans(0, length, ImageSpan.class); if (imageSpans != null) { for (int i = 0; i < imageSpans.length; i++) { ImageSpan span = imageSpans[i]; replaceSpan(builder, span, new ImageSpan(span.getDrawable(), ImageSpan.ALIGN_BASELINE)); } } // Trim the trailing new line characters at the end of the text (which can be added // when HTML block quote tags are turned into new line characters). int end = length; for (int i = builder.length() - 1; i >= 0; i--) { if (builder.charAt(i) != '\n') { break; } end = i; } // If there's no trailing newlines, just return it. if (end == length) { return builder; } // Otherwise, Return a substring of the original {@link Spanned} text // from the start index (inclusive) to the end index (exclusive). return new SpannableStringBuilder(builder, 0, end); } /** * Replaces one span with the other. */ private static void replaceSpan(SpannableStringBuilder builder, Object originalSpan, Object newSpan) { builder.setSpan(newSpan, builder.getSpanStart(originalSpan), builder.getSpanEnd(originalSpan), builder.getSpanFlags(originalSpan)); builder.removeSpan(originalSpan); } public static class StreamItemQuoteSpan extends QuoteSpan { private final int mWidth; public StreamItemQuoteSpan(int color, int width) { super(color); this.mWidth = width; } /** * {@inheritDoc} */ @Override public int getLeadingMargin(boolean first) { return mWidth; } } }