/** * Bind text view to HTML string * * @param view * @param html * @param id * @return this image getter */ public HttpImageGetter bind(final TextView view, final String html, final Object id) { if (TextUtils.isEmpty(html)) return hide(view); CharSequence encoded = fullHtmlCache.get(id); if (encoded != null) return show(view, encoded); encoded = rawHtmlCache.get(id); if (encoded == null) { encoded = HtmlUtils.encode(html, loading); if (containsImages(html)) rawHtmlCache.put(id, encoded); else { rawHtmlCache.remove(id); fullHtmlCache.put(id, encoded); return show(view, encoded); } } if (TextUtils.isEmpty(encoded)) return hide(view); show(view, encoded); view.setTag(id); ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(); AsyncTaskCompat.executeParallel(asyncTask, html, id, view); return this; }
@Override protected CharSequence doInBackground(Object... params) { html = (String) params[0]; id = params[1]; view = (TextView) params[2]; return HtmlUtils.encode(html, HttpImageGetter.this); }
/** * Encode given HTML string and map it to the given id * * @param id * @param html * @return this image getter */ public void encode(final Object id, final String html) { if (TextUtils.isEmpty(html)) return; CharSequence encoded = HtmlUtils.encode(html, loading); // Use default encoding if no img tags if (containsImages(html)) rawHtmlCache.put(id, encoded); else { rawHtmlCache.remove(id); fullHtmlCache.put(id, encoded); } }
/** * Bind text view to HTML string * * @param view * @param html * @param id * @return this image getter */ public HttpImageGetter bind(final TextView view, final String html, final Object id) { if (TextUtils.isEmpty(html)) return hide(view); CharSequence encoded = fullHtmlCache.get(id); if (encoded != null) return show(view, encoded); encoded = rawHtmlCache.get(id); if (encoded == null) { encoded = HtmlUtils.encode(html, loading); if (containsImages(html)) rawHtmlCache.put(id, encoded); else { rawHtmlCache.remove(id); fullHtmlCache.put(id, encoded); return show(view, encoded); } } if (TextUtils.isEmpty(encoded)) return hide(view); show(view, encoded); view.setTag(id); new RoboAsyncTask<CharSequence>(context) { @Override public CharSequence call() throws Exception { return HtmlUtils.encode(html, HttpImageGetter.this); } @Override protected void onSuccess(final CharSequence html) throws Exception { rawHtmlCache.remove(id); fullHtmlCache.put(id, html); if (id.equals(view.getTag())) show(view, html); } }.execute(); return this; }