mirror of https://github.com/M66B/FairEmail.git
Allow saving EML attachments
This commit is contained in:
parent
ddb476fffe
commit
d6525eaa68
|
@ -23,26 +23,36 @@ import android.Manifest;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
|
import android.content.Intent;
|
||||||
import android.content.res.AssetFileDescriptor;
|
import android.content.res.AssetFileDescriptor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.AdapterView;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.constraintlayout.widget.Group;
|
import androidx.constraintlayout.widget.Group;
|
||||||
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import com.sun.mail.imap.IMAPFolder;
|
import com.sun.mail.imap.IMAPFolder;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
@ -50,12 +60,17 @@ import java.util.Properties;
|
||||||
import javax.mail.Flags;
|
import javax.mail.Flags;
|
||||||
import javax.mail.Folder;
|
import javax.mail.Folder;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import javax.mail.internet.ContentType;
|
import javax.mail.internet.ContentType;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
|
|
||||||
public class ActivityEML extends ActivityBase {
|
public class ActivityEML extends ActivityBase {
|
||||||
private Uri uri;
|
private Uri uri;
|
||||||
|
private Result result;
|
||||||
|
private MessageHelper.AttachmentPart apart;
|
||||||
|
|
||||||
|
private static final int REQUEST_ATTACHMENT = 1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -67,11 +82,28 @@ public class ActivityEML extends ActivityBase {
|
||||||
final TextView tvTo = findViewById(R.id.tvTo);
|
final TextView tvTo = findViewById(R.id.tvTo);
|
||||||
final TextView tvFrom = findViewById(R.id.tvFrom);
|
final TextView tvFrom = findViewById(R.id.tvFrom);
|
||||||
final TextView tvSubject = findViewById(R.id.tvSubject);
|
final TextView tvSubject = findViewById(R.id.tvSubject);
|
||||||
final TextView tvAttachments = findViewById(R.id.tvAttachments);
|
final ListView lvAttachment = findViewById(R.id.lvAttachment);
|
||||||
final TextView tvBody = findViewById(R.id.tvBody);
|
final TextView tvBody = findViewById(R.id.tvBody);
|
||||||
final ContentLoadingProgressBar pbWait = findViewById(R.id.pbWait);
|
final ContentLoadingProgressBar pbWait = findViewById(R.id.pbWait);
|
||||||
final Group grpReady = findViewById(R.id.grpReady);
|
final Group grpReady = findViewById(R.id.grpReady);
|
||||||
|
|
||||||
|
lvAttachment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
|
apart = result.parts.getAttachmentParts().get(position);
|
||||||
|
|
||||||
|
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||||
|
create.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
|
create.setType(apart.attachment.getMimeType());
|
||||||
|
if (!TextUtils.isEmpty(apart.attachment.name))
|
||||||
|
create.putExtra(Intent.EXTRA_TITLE, apart.attachment.name);
|
||||||
|
if (create.resolveActivity(getPackageManager()) == null)
|
||||||
|
ToastEx.makeText(ActivityEML.this, R.string.title_no_saf, Toast.LENGTH_LONG).show();
|
||||||
|
else
|
||||||
|
startActivityForResult(Helper.getChooser(ActivityEML.this, create), REQUEST_ATTACHMENT);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
grpReady.setVisibility(View.GONE);
|
grpReady.setVisibility(View.GONE);
|
||||||
|
|
||||||
uri = getIntent().getData();
|
uri = getIntent().getData();
|
||||||
|
@ -117,23 +149,9 @@ public class ActivityEML extends ActivityBase {
|
||||||
result.from = MessageHelper.formatAddresses(helper.getFrom());
|
result.from = MessageHelper.formatAddresses(helper.getFrom());
|
||||||
result.to = MessageHelper.formatAddresses(helper.getTo());
|
result.to = MessageHelper.formatAddresses(helper.getTo());
|
||||||
result.subject = helper.getSubject();
|
result.subject = helper.getSubject();
|
||||||
|
result.parts = helper.getMessageParts();
|
||||||
|
|
||||||
MessageHelper.MessageParts parts = helper.getMessageParts();
|
String html = result.parts.getHtml(context);
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
for (MessageHelper.AttachmentPart apart : parts.getAttachmentParts()) {
|
|
||||||
if (sb.length() > 0)
|
|
||||||
sb.append("<br>");
|
|
||||||
ContentType ct = new ContentType(apart.part.getContentType());
|
|
||||||
sb.append(ct.getBaseType().toLowerCase(Locale.ROOT));
|
|
||||||
if (apart.disposition != null)
|
|
||||||
sb.append(' ').append(apart.disposition);
|
|
||||||
if (apart.filename != null)
|
|
||||||
sb.append(' ').append(apart.filename);
|
|
||||||
}
|
|
||||||
result.attachments = HtmlHelper.fromHtml(sb.toString());
|
|
||||||
|
|
||||||
String html = parts.getHtml(context);
|
|
||||||
if (html != null)
|
if (html != null)
|
||||||
result.body = HtmlHelper.fromHtml(HtmlHelper.sanitize(context, html, false));
|
result.body = HtmlHelper.fromHtml(HtmlHelper.sanitize(context, html, false));
|
||||||
|
|
||||||
|
@ -143,10 +161,32 @@ public class ActivityEML extends ActivityBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onExecuted(Bundle args, Result result) {
|
protected void onExecuted(Bundle args, Result result) {
|
||||||
|
ActivityEML.this.result = result;
|
||||||
|
|
||||||
tvFrom.setText(result.from);
|
tvFrom.setText(result.from);
|
||||||
tvTo.setText(result.to);
|
tvTo.setText(result.to);
|
||||||
tvSubject.setText(result.subject);
|
tvSubject.setText(result.subject);
|
||||||
tvAttachments.setText(result.attachments);
|
|
||||||
|
List<String> attachments = new ArrayList<>();
|
||||||
|
for (MessageHelper.AttachmentPart apart : result.parts.getAttachmentParts()) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
try {
|
||||||
|
ContentType ct = new ContentType(apart.part.getContentType());
|
||||||
|
sb.append(ct.getBaseType().toLowerCase(Locale.ROOT));
|
||||||
|
} catch (MessagingException ex) {
|
||||||
|
sb.append(ex.getMessage());
|
||||||
|
}
|
||||||
|
if (apart.disposition != null)
|
||||||
|
sb.append(' ').append(apart.disposition);
|
||||||
|
if (apart.filename != null)
|
||||||
|
sb.append(' ').append(apart.filename);
|
||||||
|
attachments.add(sb.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayAdapter adapter = new ArrayAdapter<>(
|
||||||
|
ActivityEML.this, R.layout.list_item1, android.R.id.text1, attachments);
|
||||||
|
lvAttachment.setAdapter(adapter);
|
||||||
|
|
||||||
tvBody.setText(result.body);
|
tvBody.setText(result.body);
|
||||||
grpReady.setVisibility(View.VISIBLE);
|
grpReady.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
@ -161,6 +201,81 @@ public class ActivityEML extends ActivityBase {
|
||||||
}.execute(this, args, "eml:decode");
|
}.execute(this, args, "eml:decode");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (requestCode) {
|
||||||
|
case REQUEST_ATTACHMENT:
|
||||||
|
if (resultCode == RESULT_OK && data != null)
|
||||||
|
onSaveAttachment(data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onSaveAttachment(Intent data) {
|
||||||
|
Bundle args = new Bundle();
|
||||||
|
args.putParcelable("uri", data.getData());
|
||||||
|
|
||||||
|
new SimpleTask<Void>() {
|
||||||
|
@Override
|
||||||
|
protected Void onExecute(Context context, Bundle args) throws Throwable {
|
||||||
|
Uri uri = args.getParcelable("uri");
|
||||||
|
|
||||||
|
if (!"content".equals(uri.getScheme())) {
|
||||||
|
Log.w("Save attachment uri=" + uri);
|
||||||
|
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
ParcelFileDescriptor pfd = null;
|
||||||
|
OutputStream os = null;
|
||||||
|
InputStream is;
|
||||||
|
try {
|
||||||
|
pfd = getContentResolver().openFileDescriptor(uri, "w");
|
||||||
|
os = new FileOutputStream(pfd.getFileDescriptor());
|
||||||
|
is = apart.part.getInputStream();
|
||||||
|
|
||||||
|
byte[] buffer = new byte[Helper.BUFFER_SIZE];
|
||||||
|
int read;
|
||||||
|
while ((read = is.read(buffer)) != -1)
|
||||||
|
os.write(buffer, 0, read);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (pfd != null)
|
||||||
|
pfd.close();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Log.w(ex);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (os != null)
|
||||||
|
os.close();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Log.w(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onExecuted(Bundle args, Void data) {
|
||||||
|
ToastEx.makeText(ActivityEML.this, R.string.title_attachment_saved, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onException(Bundle args, Throwable ex) {
|
||||||
|
if (ex instanceof IllegalArgumentException || ex instanceof FileNotFoundException)
|
||||||
|
ToastEx.makeText(ActivityEML.this, ex.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
|
else
|
||||||
|
Helper.unexpectedError(getSupportFragmentManager(), ex);
|
||||||
|
}
|
||||||
|
}.execute(this, args, "eml:attachment");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
MenuInflater inflater = getMenuInflater();
|
MenuInflater inflater = getMenuInflater();
|
||||||
|
@ -282,7 +397,7 @@ public class ActivityEML extends ActivityBase {
|
||||||
String from;
|
String from;
|
||||||
String to;
|
String to;
|
||||||
String subject;
|
String subject;
|
||||||
Spanned attachments;
|
MessageHelper.MessageParts parts;
|
||||||
Spanned body;
|
Spanned body;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
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-2019 by Marcel Bokhorst (M66B)
|
||||||
|
*/
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.widget.ListView;
|
||||||
|
|
||||||
|
public class ListViewEx extends ListView {
|
||||||
|
public ListViewEx(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListViewEx(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListViewEx(Context context, AttributeSet attrs, int defStyle) {
|
||||||
|
super(context, attrs, defStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
|
super.onMeasure(
|
||||||
|
widthMeasureSpec,
|
||||||
|
MeasureSpec.makeMeasureSpec(
|
||||||
|
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST));
|
||||||
|
}
|
||||||
|
}
|
|
@ -103,17 +103,13 @@
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/tvSubject" />
|
app:layout_constraintTop_toBottomOf="@id/tvSubject" />
|
||||||
|
|
||||||
<TextView
|
<eu.faircode.email.ListViewEx
|
||||||
android:id="@+id/tvAttachments"
|
android:id="@+id/lvAttachment"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="3dp"
|
|
||||||
android:text="Attachments"
|
|
||||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
|
||||||
android:textIsSelectable="true"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/vSeparatorAttachments" />
|
app:layout_constraintTop_toBottomOf="@+id/vSeparatorAttachments" />
|
||||||
|
|
||||||
<View
|
<View
|
||||||
android:id="@+id/vSeparatorBody"
|
android:id="@+id/vSeparatorBody"
|
||||||
|
@ -123,7 +119,7 @@
|
||||||
android:background="?attr/colorSeparator"
|
android:background="?attr/colorSeparator"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/tvAttachments" />
|
app:layout_constraintTop_toBottomOf="@id/lvAttachment" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tvBody"
|
android:id="@+id/tvBody"
|
||||||
|
@ -146,7 +142,7 @@
|
||||||
vSeparatorHeader,tvFromTitle,tvFrom,
|
vSeparatorHeader,tvFromTitle,tvFrom,
|
||||||
tvToTitle,tvTo,
|
tvToTitle,tvTo,
|
||||||
tvSubject,
|
tvSubject,
|
||||||
vSeparatorAttachments,tvAttachments,
|
vSeparatorAttachments,lvAttachment,
|
||||||
vSeparatorBody,tvBody" />
|
vSeparatorBody,tvBody" />
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:padding="3dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@android:id/text1"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Text1"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue