<commit_before>package eu.e43.impeller.uikit;

import java.net.URI;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.widget.TextView;

import eu.e43.impeller.activity.ActivityWithAccount;

public class PumpHtml implements ImageLoader.Listener, ImageGetter {
	private static final String TAG = "PumpHtml";
	
	private ImageLoader			m_loader;
	private String   			m_html;
	private TextView 			m_view;
	private int      			m_outstanding = 0;
    private boolean             m_done = false;
	
	public static void setFromHtml(ActivityWithAccount ctx, TextView view, String html) {
		new PumpHtml(ctx, view, html).parse();
	}
	
	private PumpHtml(ActivityWithAccount ctx, TextView view, String html) {
		m_loader = ctx.getImageLoader();
		m_view   = view;
		m_html   = html;
	}
	
	private void parse() {
		m_view.setText(Html.fromHtml(m_html, this, null));
	}
	
	@Override
	public Drawable getDrawable(String url) {
		Drawable d = m_loader.getCachedImage(url);
		if(d != null) {
			Log.v(TAG, "getDrawable(" + url + ") successful");
			return d;
		} else {
			Log.v(TAG, "getDrawable(" + url + ") pending");
			m_outstanding++;
            if(!m_done) m_loader.load(this, url);
			return null;
		}
	}
	
	@Override
	public void loaded(BitmapDrawable dr, URI uri) {
		m_outstanding--;	
		Log.v(TAG, "loaded(" + uri + ") -> " + m_outstanding + " outstanding");
		if(m_outstanding == 0) {
            m_done = true;
            parse();
        }
	}

	@Override
	public void error(URI uri) {
		Log.w(TAG, "error(" + uri + ")");
        m_outstanding--;
        if(m_outstanding == 0) {
            m_done = true;
            parse();
        }
	}

}<commit_msg>PumpHtml won't reparse the content any more. Yay!<commit_after>package eu.e43.impeller.uikit;

import java.net.URI;
import java.net.URISyntaxException;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;
import android.widget.TextView;

import eu.e43.impeller.activity.ActivityWithAccount;

public class PumpHtml implements ImageLoader.Listener, ImageGetter {
	private static final String TAG = "PumpHtml";
	
	private ImageLoader			    m_loader;
	private TextView 			    m_view;
    private SpannableStringBuilder  m_builder;

	public static void setFromHtml(ActivityWithAccount ctx, TextView view, String html) {
		new PumpHtml(ctx, view, html);
	}
	
	private PumpHtml(ActivityWithAccount ctx, TextView view, String html) {
		m_loader  = ctx.getImageLoader();
		m_view    = view;
        m_builder = SpannableStringBuilder.valueOf(Html.fromHtml(html, this, null));
        m_view.setText(m_builder);
	}
	
	@Override
	public Drawable getDrawable(String url) {
		Drawable d = m_loader.getCachedImage(url);
		if(d != null) {
			Log.v(TAG, "getDrawable(" + url + ") successful");
			return d;
		} else {
			Log.v(TAG, "getDrawable(" + url + ") pending");
            m_loader.load(this, url);
			return null;
		}
	}
	
	@Override
	public void loaded(BitmapDrawable dr, URI uri) {

		Log.v(TAG, "loaded(" + uri + ")");

        Editable spanned = m_builder;
        ImageSpan[] imgs = spanned.getSpans(0, spanned.length(), ImageSpan.class);

        for(ImageSpan img : imgs) {
            URI asUri;
            try {
                asUri = new URI(img.getSource());
            } catch (URISyntaxException e) {
                Log.e(TAG, "Error parsing image URI", e);
                continue;
            }

            if(asUri.equals(uri)) {
                ImageSpan newSpan = new ImageSpan(dr, img.getSource());
                spanned.setSpan(newSpan, spanned.getSpanStart(img), spanned.getSpanEnd(img),
                        spanned.getSpanFlags(img));
                spanned.removeSpan(img);
            }
        }

        m_view.setText(m_builder);
	}

	@Override
	public void error(URI uri) {
		Log.w(TAG, "error(" + uri + ")");
	}

}
{'fraction_non_alphanumeric': 0.11743772241992882, 'fraction_numerical': 0.0028469750889679717, 'mean_word_length': 2.9773584905660377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5381876', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1343, 'n_words': 305}