mirror of https://github.com/M66B/FairEmail.git
Added save code
This commit is contained in:
parent
e38e4d36ad
commit
f14fc75dc5
|
@ -19,6 +19,7 @@ package eu.faircode.email;
|
|||
Copyright 2018-2023 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
|
@ -35,6 +36,7 @@ import android.view.View;
|
|||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.OnBackPressedCallback;
|
||||
import androidx.annotation.NonNull;
|
||||
|
@ -46,6 +48,10 @@ import org.jsoup.nodes.Element;
|
|||
import org.w3c.dom.css.CSSStyleSheet;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -54,9 +60,11 @@ public class ActivityCode extends ActivityBase {
|
|||
private ContentLoadingProgressBar pbWait;
|
||||
private Group grpReady;
|
||||
|
||||
private boolean lines = BuildConfig.DEBUG;
|
||||
private boolean lines = false;
|
||||
private boolean links = false;
|
||||
private boolean sanitize = BuildConfig.DEBUG;
|
||||
private boolean sanitize = false;
|
||||
|
||||
private static final int REQUEST_SAVE = 1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -151,18 +159,6 @@ public class ActivityCode extends ActivityBase {
|
|||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
boolean debug = prefs.getBoolean("debug", false);
|
||||
|
||||
menu.findItem(R.id.menu_lines)
|
||||
.setChecked(lines)
|
||||
.setIcon(lines
|
||||
? R.drawable.twotone_speaker_notes_off_24
|
||||
: R.drawable.twotone_speaker_notes_24);
|
||||
|
||||
menu.findItem(R.id.menu_links)
|
||||
.setChecked(links)
|
||||
.setIcon(links
|
||||
? R.drawable.twotone_link_off_24
|
||||
: R.drawable.twotone_link_24);
|
||||
|
||||
menu.findItem(R.id.menu_sanitize)
|
||||
.setVisible(BuildConfig.DEBUG || debug)
|
||||
.setChecked(sanitize)
|
||||
|
@ -173,6 +169,8 @@ public class ActivityCode extends ActivityBase {
|
|||
? R.string.title_legend_show_full
|
||||
: R.string.title_legend_show_reformatted));
|
||||
|
||||
menu.findItem(R.id.menu_lines).setChecked(lines);
|
||||
menu.findItem(R.id.menu_links).setChecked(links);
|
||||
|
||||
return super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
@ -180,28 +178,47 @@ public class ActivityCode extends ActivityBase {
|
|||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
int itemId = item.getItemId();
|
||||
if (itemId == android.R.id.home) {
|
||||
finishAndRemoveTask();
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_lines) {
|
||||
lines = !lines;
|
||||
invalidateOptionsMenu();
|
||||
load();
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_links) {
|
||||
links = !links;
|
||||
invalidateOptionsMenu();
|
||||
load();
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_sanitize) {
|
||||
if (itemId == R.id.menu_sanitize) {
|
||||
sanitize = !sanitize;
|
||||
invalidateOptionsMenu();
|
||||
load();
|
||||
return true;
|
||||
} else if (itemId == android.R.id.home) {
|
||||
finishAndRemoveTask();
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_lines) {
|
||||
lines = !lines;
|
||||
item.setChecked(lines);
|
||||
load();
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_links) {
|
||||
links = !links;
|
||||
item.setChecked(links);
|
||||
load();
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_save) {
|
||||
selectFile();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
private void load() {
|
||||
Intent intent = getIntent();
|
||||
long id = intent.getLongExtra("id", -1L);
|
||||
|
@ -304,6 +321,82 @@ public class ActivityCode extends ActivityBase {
|
|||
protected void onException(Bundle args, @NonNull Throwable ex) {
|
||||
Log.unexpectedError(getSupportFragmentManager(), ex, false);
|
||||
}
|
||||
}.execute(this, args, "view:text");
|
||||
}.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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M17,7h-4v2h4c1.65,0 3,1.35 3,3s-1.35,3 -3,3h-4v2h4c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5zM11,15L7,15c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3h4L11,7L7,7c-2.76,0 -5,2.24 -5,5s2.24,5 5,5h4v-2zM8,11h8v2L8,13z"
|
||||
android:strokeAlpha="0.87"
|
||||
android:fillAlpha="0.87"/>
|
||||
</vector>
|
|
@ -1,10 +0,0 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M14.39,11L16,12.61V11zM17,7h-4v1.9h4c1.71,0 3.1,1.39 3.1,3.1 0,1.27 -0.77,2.37 -1.87,2.84l1.4,1.4C21.05,15.36 22,13.79 22,12c0,-2.76 -2.24,-5 -5,-5zM2,4.27l3.11,3.11C3.29,8.12 2,9.91 2,12c0,2.76 2.24,5 5,5h4v-1.9H7c-1.71,0 -3.1,-1.39 -3.1,-3.1 0,-1.59 1.21,-2.9 2.76,-3.07L8.73,11H8v2h2.73L13,15.27V17h1.73l4.01,4.01 1.41,-1.41L3.41,2.86 2,4.27z"/>
|
||||
</vector>
|
|
@ -1,16 +0,0 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:autoMirrored="true">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M4,17.17l0.59,-0.59 0.58,-0.58L20,16L20,4L4,4v13.17zM10,6h8v2h-8L10,6zM10,9h8v2h-8L10,9zM10,12h5v2h-5v-2zM6,6h2v2L6,8L6,6zM6,9h2v2L6,11L6,9zM6,12h2v2L6,14v-2z"
|
||||
android:strokeAlpha="0.3"
|
||||
android:fillAlpha="0.3"/>
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM20,16L5.17,16l-0.59,0.59 -0.58,0.58L4,4h16v12zM6,12h2v2L6,14zM6,9h2v2L6,11zM6,6h2v2L6,8zM10,12h5v2h-5zM10,9h8v2h-8zM10,6h8v2h-8z"/>
|
||||
</vector>
|
|
@ -1,15 +0,0 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M6,11L6,9L4,7v10.17L5.17,16L13,16l-5,-5L6,11zM8,14L6,14v-2h2v2zM20,4L6.66,4L10,7.34L10,6h8v2h-7.34l1,1L18,9v2h-4.34l5,5L20,16z"
|
||||
android:strokeAlpha="0.3"
|
||||
android:fillAlpha="0.3"/>
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M20,4v12h-1.34l1.91,1.91C21.39,17.66 22,16.9 22,16L22,4c0,-1.1 -0.9,-2 -2,-2L4.66,2l2,2L20,4zM6,12h2v2L6,14zM18,11L18,9h-6.34l2,2zM18,8L18,6h-8v1.34l0.66,0.66zM1.41,1.59L0,3l2,2.01L2,22l4,-4h9l5.73,5.73 1.41,-1.41L1.41,1.59zM5.17,16L4,17.17L4,7l2,2v2h2l5,5L5.17,16z"/>
|
||||
</vector>
|
|
@ -1,24 +1,27 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/menu_lines"
|
||||
android:checkable="true"
|
||||
android:icon="@drawable/twotone_speaker_notes_24"
|
||||
android:title="@string/title_legend_line_numbers"
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_links"
|
||||
android:checkable="true"
|
||||
android:icon="@drawable/twotone_link_24"
|
||||
android:title="@string/title_legend_enable_links"
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_sanitize"
|
||||
android:checkable="true"
|
||||
android:icon="@drawable/twotone_fullscreen_exit_24"
|
||||
android:title="@string/title_legend_show_reformatted"
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_lines"
|
||||
android:checkable="true"
|
||||
android:title="@string/title_legend_line_numbers"
|
||||
app:showAsAction="never" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_links"
|
||||
android:checkable="true"
|
||||
android:title="@string/title_legend_enable_links"
|
||||
app:showAsAction="never" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_save"
|
||||
android:title="@string/title_save"
|
||||
app:showAsAction="never" />
|
||||
</menu>
|
||||
|
|
Loading…
Reference in New Issue