sheets =
HtmlHelper.parseStyles(d.head().select("style"));
for (Element element : d.select("*")) {
String computed = HtmlHelper.processStyles(context,
element.tagName(),
element.className(),
element.attr("style"),
sheets);
if (!TextUtils.isEmpty(computed))
element.attr("x-computed", computed);
}
d = HtmlHelper.sanitizeView(context, d, false);
}
d.outputSettings()
.prettyPrint(true)
.outline(true)
.indentAmount(1);
if (selected == null)
return d.html();
else
return d.body().html();
}
@Override
protected void onExecuted(Bundle args, String code) {
getSupportActionBar().setSubtitle(args.getString("subject"));
String clazz = "language-html";
if (lines)
clazz += " line-numbers";
String html = "" +
"" +
"" +
" " +
" " +
" " +
(links ? " " : "") +
" " +
"" +
"" +
" " +
(links ? " " : "") +
" " + Html.escapeHtml(code) + "
" +
"" +
"";
wvCode.loadDataWithBaseURL("file:///android_asset/", html, "text/html", StandardCharsets.UTF_8.name(), null);
grpReady.setVisibility(View.VISIBLE);
}
@Override
protected void onException(Bundle args, @NonNull Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex, false);
}
}.execute(this, args, "code:view");
}
private void selectFile() {
long id = getIntent().getLongExtra("id", -1L);
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, Long.toString(id) + ".html");
Helper.openAdvanced(this, intent);
startActivityForResult(intent, REQUEST_SAVE);
}
private void save(Intent data) {
long id = getIntent().getLongExtra("id", -1L);
Bundle args = new Bundle();
args.putLong("id", id);
args.putParcelable("uri", data.getData());
new SimpleTask() {
private Toast toast = null;
@Override
protected void onPreExecute(Bundle args) {
toast = ToastEx.makeText(ActivityCode.this, R.string.title_executing, Toast.LENGTH_LONG);
toast.show();
}
@Override
protected void onPostExecute(Bundle args) {
if (toast != null)
toast.cancel();
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
Uri uri = args.getParcelable("uri");
if (uri == null)
throw new FileNotFoundException();
if (!"content".equals(uri.getScheme())) {
Log.w("Export uri=" + uri);
throw new IllegalArgumentException(uri.getScheme());
}
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null)
return null;
File file = message.getFile(context);
ContentResolver resolver = context.getContentResolver();
try (OutputStream os = resolver.openOutputStream(uri)) {
try (InputStream is = new FileInputStream(file)) {
Helper.copy(is, os);
}
}
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(ActivityCode.this, R.string.title_completed, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, @NonNull Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex, false);
}
}.execute(this, args, "code:save");
}
}