public void load(@Nullable final Uri uri, @NonNull final OnMarkdownTextLoaded loaded) { cancel(); task = service.submit(new Runnable() { @Override public void run() { try { deliver(loaded, text(uri)); } catch (Throwable t) { Debug.e(t); } } }); }
private void deliver(@NonNull final OnMarkdownTextLoaded loaded, final String text) { if (!isCancelled()) { handler.post(new Runnable() { @Override public void run() { // as this call is async, we need to check again if we are cancelled if (!isCancelled()) { loaded.apply(text); task = null; } } }); } }
private String loadExternalResource(@NonNull Uri uri) { final String out; final String scheme = uri.getScheme(); if (!TextUtils.isEmpty(scheme) && ContentResolver.SCHEME_FILE.equals(scheme)) { out = loadExternalFile(uri); } else { out = loadExternalUrl(uri); } return out; }
private String text(@Nullable Uri uri) { final String out; if (uri == null) { out = loadReadMe(); } else { out = loadExternalResource(uri); } return out; }
private String loadReadMe() { InputStream stream = null; try { stream = context.getAssets().open("README.md"); } catch (IOException e) { Debug.e(e); } return readStream(stream); }
markdownLoader.load(uri(), new MarkdownLoader.OnMarkdownTextLoaded() { @Override public void apply(final String text) {
private String loadExternalFile(@NonNull Uri uri) { InputStream stream = null; try { stream = new FileInputStream(new File(uri.getPath())); } catch (FileNotFoundException e) { Debug.e(e); } return readStream(stream); }
@Override protected void onDestroy() { super.onDestroy(); markdownLoader.cancel(); markdownRenderer.cancel(); } }