2023-11-02 20:25:35 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018-2023 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2023-11-04 08:44:38 +00:00
|
|
|
import android.content.ContentResolver;
|
2023-11-02 20:25:35 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2023-11-03 17:15:54 +00:00
|
|
|
import android.net.Uri;
|
2023-11-02 20:25:35 +00:00
|
|
|
import android.os.Bundle;
|
2023-11-03 08:40:36 +00:00
|
|
|
import android.text.Html;
|
2023-11-04 06:43:09 +00:00
|
|
|
import android.text.Spanned;
|
2023-11-02 20:25:35 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2023-11-03 08:40:36 +00:00
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
2023-11-03 17:15:54 +00:00
|
|
|
import android.webkit.WebViewClient;
|
2023-11-04 08:44:38 +00:00
|
|
|
import android.widget.Toast;
|
2023-11-02 20:25:35 +00:00
|
|
|
|
2023-11-02 21:26:39 +00:00
|
|
|
import androidx.activity.OnBackPressedCallback;
|
2023-11-02 20:25:35 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2023-11-05 07:20:43 +00:00
|
|
|
import androidx.appcompat.widget.SearchView;
|
2023-11-02 20:25:35 +00:00
|
|
|
import androidx.constraintlayout.widget.Group;
|
|
|
|
import androidx.preference.PreferenceManager;
|
2023-11-05 06:59:19 +00:00
|
|
|
import androidx.webkit.WebSettingsCompat;
|
|
|
|
import androidx.webkit.WebViewFeature;
|
2023-11-02 20:25:35 +00:00
|
|
|
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
import org.w3c.dom.css.CSSStyleSheet;
|
|
|
|
|
|
|
|
import java.io.File;
|
2023-11-04 08:44:38 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2023-11-03 08:40:36 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2023-11-02 20:25:35 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2023-11-03 08:40:36 +00:00
|
|
|
public class ActivityCode extends ActivityBase {
|
|
|
|
private WebView wvCode;
|
2023-11-02 20:25:35 +00:00
|
|
|
private ContentLoadingProgressBar pbWait;
|
|
|
|
private Group grpReady;
|
|
|
|
|
2023-11-05 06:59:19 +00:00
|
|
|
private boolean force_light = false;
|
|
|
|
private boolean sanitize = false;
|
2023-11-04 08:44:38 +00:00
|
|
|
private boolean lines = false;
|
2023-11-03 17:15:54 +00:00
|
|
|
private boolean links = false;
|
2023-11-05 07:20:43 +00:00
|
|
|
private String searching = null;
|
2023-11-04 08:44:38 +00:00
|
|
|
|
|
|
|
private static final int REQUEST_SAVE = 1;
|
2023-11-02 20:25:35 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2023-11-03 11:59:56 +00:00
|
|
|
if (savedInstanceState != null) {
|
2023-11-05 06:59:19 +00:00
|
|
|
force_light = savedInstanceState.getBoolean("fair:force_light");
|
|
|
|
sanitize = savedInstanceState.getBoolean("fair:sanitize");
|
2023-11-03 11:59:56 +00:00
|
|
|
lines = savedInstanceState.getBoolean("fair:lines");
|
2023-11-03 17:15:54 +00:00
|
|
|
links = savedInstanceState.getBoolean("fair:links");
|
2023-11-05 07:20:43 +00:00
|
|
|
searching = savedInstanceState.getString("fair:searching");
|
2023-11-03 11:59:56 +00:00
|
|
|
}
|
2023-11-02 20:25:35 +00:00
|
|
|
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
2023-11-02 21:26:39 +00:00
|
|
|
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
|
|
|
|
@Override
|
|
|
|
public void handleOnBackPressed() {
|
|
|
|
finishAndRemoveTask();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-03 08:40:36 +00:00
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.activity_code, null);
|
2023-11-02 20:25:35 +00:00
|
|
|
setContentView(view);
|
|
|
|
|
2023-11-03 08:40:36 +00:00
|
|
|
wvCode = findViewById(R.id.wvCode);
|
2023-11-02 20:25:35 +00:00
|
|
|
pbWait = findViewById(R.id.pbWait);
|
|
|
|
grpReady = findViewById(R.id.grpReady);
|
|
|
|
|
2023-11-03 08:40:36 +00:00
|
|
|
WebSettings settings = wvCode.getSettings();
|
2023-11-03 10:55:54 +00:00
|
|
|
|
|
|
|
settings.setBuiltInZoomControls(true);
|
|
|
|
settings.setDisplayZoomControls(false);
|
|
|
|
|
|
|
|
settings.setAllowFileAccess(false);
|
|
|
|
settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
|
|
|
|
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
|
|
|
|
|
|
|
|
settings.setLoadsImagesAutomatically(false);
|
|
|
|
settings.setBlockNetworkLoads(true);
|
|
|
|
settings.setBlockNetworkImage(true);
|
2023-11-03 08:40:36 +00:00
|
|
|
settings.setJavaScriptEnabled(true);
|
|
|
|
|
2023-11-05 06:59:19 +00:00
|
|
|
setDarkMode();
|
|
|
|
|
2023-11-03 17:15:54 +00:00
|
|
|
wvCode.setWebViewClient(new WebViewClient() {
|
|
|
|
@Override
|
|
|
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putParcelable("uri", Uri.parse(url));
|
|
|
|
args.putString("title", null);
|
|
|
|
args.putBoolean("always_confirm", true);
|
|
|
|
|
|
|
|
FragmentDialogOpenLink fragment = new FragmentDialogOpenLink();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.show(getSupportFragmentManager(), "open:link");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-03 17:17:42 +00:00
|
|
|
|
2023-11-03 17:30:50 +00:00
|
|
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
|
Log.w("View code error " + errorCode + ":" + description);
|
2023-11-03 17:17:42 +00:00
|
|
|
}
|
2023-11-03 17:15:54 +00:00
|
|
|
});
|
|
|
|
|
2023-11-02 20:25:35 +00:00
|
|
|
// Initialize
|
|
|
|
grpReady.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
2023-11-05 06:59:19 +00:00
|
|
|
outState.putBoolean("fair:force_light", force_light);
|
|
|
|
outState.putBoolean("fair:sanitize", sanitize);
|
2023-11-03 11:59:56 +00:00
|
|
|
outState.putBoolean("fair:lines", lines);
|
2023-11-03 17:15:54 +00:00
|
|
|
outState.putBoolean("fair:links", links);
|
2023-11-05 07:20:43 +00:00
|
|
|
outState.putString("fair:searching", searching);
|
2023-11-02 20:25:35 +00:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
2023-11-03 08:40:36 +00:00
|
|
|
inflater.inflate(R.menu.menu_code, menu);
|
2023-11-05 07:20:43 +00:00
|
|
|
|
|
|
|
final String saved = searching;
|
|
|
|
final MenuItem menuSearch = menu.findItem(R.id.menu_search);
|
|
|
|
final SearchView searchView = (SearchView) menuSearch.getActionView();
|
|
|
|
|
|
|
|
if (searchView != null)
|
|
|
|
searchView.setQueryHint(getString(R.string.title_search));
|
|
|
|
|
|
|
|
if (searchView != null) {
|
|
|
|
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String query) {
|
|
|
|
search(query);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String newText) {
|
|
|
|
search(newText);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void search(String query) {
|
|
|
|
searching = query;
|
|
|
|
if (wvCode != null)
|
|
|
|
wvCode.findAllAsync(query);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(saved)) {
|
|
|
|
menuSearch.expandActionView();
|
|
|
|
searchView.setQuery(saved, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:25:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
boolean debug = prefs.getBoolean("debug", false);
|
|
|
|
|
2023-11-05 06:59:19 +00:00
|
|
|
boolean dark = Helper.isDarkTheme(this);
|
|
|
|
boolean canDarken = WebViewEx.isFeatureSupported(this, WebViewFeature.ALGORITHMIC_DARKENING);
|
|
|
|
menu.findItem(R.id.menu_force_light)
|
|
|
|
.setVisible(dark && canDarken)
|
|
|
|
.getIcon().setLevel(force_light ? 1 : 0);
|
|
|
|
|
2023-11-02 20:25:35 +00:00
|
|
|
menu.findItem(R.id.menu_sanitize)
|
|
|
|
.setVisible(BuildConfig.DEBUG || debug)
|
2023-11-03 10:18:34 +00:00
|
|
|
.setIcon(sanitize
|
|
|
|
? R.drawable.twotone_fullscreen_24
|
|
|
|
: R.drawable.twotone_fullscreen_exit_24)
|
|
|
|
.setTitle(getString(sanitize
|
|
|
|
? R.string.title_legend_show_full
|
|
|
|
: R.string.title_legend_show_reformatted));
|
2023-11-03 11:59:56 +00:00
|
|
|
|
2023-11-04 08:44:38 +00:00
|
|
|
menu.findItem(R.id.menu_lines).setChecked(lines);
|
|
|
|
menu.findItem(R.id.menu_links).setChecked(links);
|
2023-11-03 11:59:56 +00:00
|
|
|
|
2023-11-02 20:25:35 +00:00
|
|
|
return super.onPrepareOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int itemId = item.getItemId();
|
2023-11-05 06:59:19 +00:00
|
|
|
if (itemId == R.id.menu_force_light) {
|
|
|
|
force_light = !force_light;
|
2023-11-05 07:20:43 +00:00
|
|
|
item.getIcon().setLevel(force_light ? 1 : 0);
|
2023-11-05 06:59:19 +00:00
|
|
|
setDarkMode();
|
|
|
|
return true;
|
|
|
|
} else if (itemId == R.id.menu_sanitize) {
|
2023-11-04 08:44:38 +00:00
|
|
|
sanitize = !sanitize;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
load();
|
|
|
|
return true;
|
|
|
|
} else if (itemId == android.R.id.home) {
|
2023-11-02 21:26:39 +00:00
|
|
|
finishAndRemoveTask();
|
2023-11-02 20:25:35 +00:00
|
|
|
return true;
|
2023-11-03 11:59:56 +00:00
|
|
|
} else if (itemId == R.id.menu_lines) {
|
|
|
|
lines = !lines;
|
2023-11-04 08:44:38 +00:00
|
|
|
item.setChecked(lines);
|
2023-11-03 11:59:56 +00:00
|
|
|
load();
|
|
|
|
return true;
|
2023-11-03 17:15:54 +00:00
|
|
|
} else if (itemId == R.id.menu_links) {
|
|
|
|
links = !links;
|
2023-11-04 08:44:38 +00:00
|
|
|
item.setChecked(links);
|
2023-11-03 17:15:54 +00:00
|
|
|
load();
|
|
|
|
return true;
|
2023-11-04 08:44:38 +00:00
|
|
|
} else if (itemId == R.id.menu_save) {
|
|
|
|
selectFile();
|
2023-11-02 20:25:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2023-11-04 08:44:38 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (requestCode) {
|
|
|
|
case REQUEST_SAVE:
|
|
|
|
if (resultCode == RESULT_OK)
|
|
|
|
save(data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-05 06:59:19 +00:00
|
|
|
private void setDarkMode() {
|
|
|
|
WebSettings settings = wvCode.getSettings();
|
|
|
|
boolean dark = (Helper.isDarkTheme(this) && !force_light);
|
|
|
|
boolean canDarken = WebViewEx.isFeatureSupported(this, WebViewFeature.ALGORITHMIC_DARKENING);
|
|
|
|
if (canDarken)
|
|
|
|
WebSettingsCompat.setAlgorithmicDarkeningAllowed(settings, dark);
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:25:35 +00:00
|
|
|
private void load() {
|
|
|
|
Intent intent = getIntent();
|
|
|
|
long id = intent.getLongExtra("id", -1L);
|
2023-11-04 06:43:09 +00:00
|
|
|
CharSequence selected = intent.getCharSequenceExtra("selected");
|
|
|
|
Log.i("Show code message=" + id + " selected=" + (selected != null) +
|
|
|
|
" lines=" + lines + " links=" + links + " sanitize=" + sanitize);
|
2023-11-02 20:25:35 +00:00
|
|
|
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", id);
|
2023-11-04 06:43:09 +00:00
|
|
|
args.putCharSequence("selected", selected);
|
2023-11-02 20:25:35 +00:00
|
|
|
args.putBoolean("sanitize", sanitize);
|
|
|
|
|
|
|
|
new SimpleTask<String>() {
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String onExecute(Context context, Bundle args) throws Throwable {
|
|
|
|
long id = args.getLong("id");
|
2023-11-04 06:43:09 +00:00
|
|
|
CharSequence selected = args.getCharSequence("selected");
|
2023-11-02 20:25:35 +00:00
|
|
|
boolean sanitize = args.getBoolean("sanitize");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
|
|
|
if (message == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
args.putString("subject", message.subject);
|
|
|
|
|
2023-11-04 06:43:09 +00:00
|
|
|
Document d;
|
|
|
|
if (selected == null) {
|
|
|
|
File file = message.getFile(context);
|
|
|
|
d = JsoupEx.parse(file);
|
|
|
|
} else {
|
|
|
|
String html = HtmlHelper.toHtml((Spanned) selected, context);
|
|
|
|
d = JsoupEx.parse(html);
|
|
|
|
}
|
2023-11-02 20:25:35 +00:00
|
|
|
|
2023-11-03 08:40:36 +00:00
|
|
|
if (sanitize) {
|
2023-11-02 20:25:35 +00:00
|
|
|
List<CSSStyleSheet> 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);
|
2023-11-03 08:40:36 +00:00
|
|
|
}
|
2023-11-02 20:25:35 +00:00
|
|
|
|
2023-11-04 17:19:06 +00:00
|
|
|
d.outputSettings()
|
|
|
|
.prettyPrint(true)
|
|
|
|
.outline(true)
|
|
|
|
.indentAmount(1);
|
|
|
|
|
2023-11-04 06:43:09 +00:00
|
|
|
if (selected == null)
|
|
|
|
return d.html();
|
|
|
|
else
|
|
|
|
return d.body().html();
|
2023-11-02 20:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-11-03 08:40:36 +00:00
|
|
|
protected void onExecuted(Bundle args, String code) {
|
2023-11-02 20:25:35 +00:00
|
|
|
getSupportActionBar().setSubtitle(args.getString("subject"));
|
|
|
|
|
2023-11-03 11:59:56 +00:00
|
|
|
String clazz = "language-html";
|
|
|
|
if (lines)
|
|
|
|
clazz += " line-numbers";
|
|
|
|
|
2023-11-03 08:40:36 +00:00
|
|
|
String html = "<!DOCTYPE html>" +
|
|
|
|
"<html>" +
|
|
|
|
"<head>" +
|
2023-11-03 11:59:56 +00:00
|
|
|
" <meta charset=\"utf-8\" />" +
|
2023-11-04 07:42:45 +00:00
|
|
|
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=0.5\" />" +
|
2023-11-03 11:59:56 +00:00
|
|
|
" <link href=\"file:///android_asset/prism.css\" rel=\"stylesheet\" />" +
|
2023-11-03 17:15:54 +00:00
|
|
|
(links ? " <link href=\"file:///android_asset/prism-autolinker.min.css\" rel=\"stylesheet\" />" : "") +
|
2023-11-03 11:59:56 +00:00
|
|
|
" <style>" +
|
2023-11-05 06:50:30 +00:00
|
|
|
" body { margin: 0 !important; font-size: smaller !important; }" +
|
|
|
|
" pre { margin-top: 0 !important; margin-bottom: 0 !important }" +
|
2023-11-03 11:59:56 +00:00
|
|
|
" </style>" +
|
2023-11-03 08:40:36 +00:00
|
|
|
"</head>" +
|
|
|
|
"<body>" +
|
2023-11-03 11:59:56 +00:00
|
|
|
" <script src=\"file:///android_asset/prism.js\"></script>" +
|
2023-11-03 17:15:54 +00:00
|
|
|
(links ? " <script src=\"file:///android_asset/prism-autolinker.min.js\"></script>" : "") +
|
2023-11-03 11:59:56 +00:00
|
|
|
" <pre><code class=\"" + clazz + "\">" + Html.escapeHtml(code) + "</code></pre>" +
|
2023-11-03 08:40:36 +00:00
|
|
|
"</body>" +
|
|
|
|
"</html>";
|
|
|
|
|
|
|
|
wvCode.loadDataWithBaseURL("file:///android_asset/", html, "text/html", StandardCharsets.UTF_8.name(), null);
|
2023-11-02 20:25:35 +00:00
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, @NonNull Throwable ex) {
|
|
|
|
Log.unexpectedError(getSupportFragmentManager(), ex, false);
|
|
|
|
}
|
2023-11-04 08:44:38 +00:00
|
|
|
}.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<Void>() {
|
|
|
|
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");
|
2023-11-02 20:25:35 +00:00
|
|
|
}
|
|
|
|
}
|