sheets =
HtmlSanitize.parseStyles(d.head().select("style"));
for (Element element : d.select("*")) {
String computed = HtmlSanitize.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(pretty)
.outline(pretty)
.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 checkHtml() {
Intent intent = getIntent();
long id = intent.getLongExtra("id", -1L);
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask() {
@Override
protected ParseErrorList onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
File file = EntityMessage.getFile(context, id);
Parser parser = Parser.htmlParser().setTrackErrors(20);
Jsoup.parse(file, StandardCharsets.UTF_8.name(), "", parser);
return parser.getErrors();
}
@Override
protected void onExecuted(Bundle args, ParseErrorList errors) {
lines = true;
pretty = false;
load();
SpannableStringBuilderEx ssb = new SpannableStringBuilderEx();
ssb.append("Errors: ")
.append(Integer.toString(errors.size()))
.append("\n\n");
for (ParseError error : errors)
ssb.append("At ")
.append(error.getCursorPos())
.append(' ')
.append(error.getErrorMessage())
.append("\n\n");
new AlertDialog.Builder(ActivityCode.this)
.setIcon(R.drawable.twotone_bug_report_24)
.setTitle(R.string.title_check_html)
.setMessage(ssb)
.setPositiveButton(android.R.string.ok, null)
.show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}.execute(this, args, "code:check");
}
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");
}
}