Improved TNEF error handling

pull/207/head
M66B 4 years ago
parent 918a1530fd
commit 8a92201c41

@ -25,6 +25,7 @@ import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -32,6 +33,7 @@ import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
@ -207,8 +209,11 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
if (attachment.available) if (attachment.available)
onShare(attachment); onShare(attachment);
else { else {
if (attachment.progress == null && attachment.subsequence == null) if (attachment.progress == null)
onDownload(attachment); if (attachment.subsequence == null)
onDownload(attachment);
else if (!TextUtils.isEmpty(attachment.error))
ToastEx.makeText(context, attachment.error, Toast.LENGTH_LONG).show();
} }
} }
} }

@ -4745,7 +4745,9 @@ class Core {
} }
for (EntityAttachment attachment : attachments) for (EntityAttachment attachment : attachments)
if (!attachment.available && TextUtils.isEmpty(attachment.error)) if (!attachment.available &&
attachment.subsequence == null &&
TextUtils.isEmpty(attachment.error))
if (state.getNetworkState().isUnmetered() || if (state.getNetworkState().isUnmetered() ||
(attachment.size != null && attachment.size < maxSize)) (attachment.size != null && attachment.size < maxSize))
try { try {

@ -3604,8 +3604,13 @@ public class MessageHelper {
attachment.disposition = Part.ATTACHMENT; attachment.disposition = Part.ATTACHMENT;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
Helper.writeText(attachment.getFile(context), subject); try {
db.attachment().setDownloaded(attachment.id, (long) subject.length()); Helper.writeText(attachment.getFile(context), subject);
db.attachment().setDownloaded(attachment.id, (long) subject.length());
} catch (Throwable ex) {
Log.e(ex);
db.attachment().setError(attachment.id, Log.formatThrowable(ex));
}
} }
String body = msg.getBody(); String body = msg.getBody();
@ -3629,9 +3634,14 @@ public class MessageHelper {
attachment.disposition = Part.ATTACHMENT; attachment.disposition = Part.ATTACHMENT;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
byte[] data = attr.getData(); try {
Helper.writeText(attachment.getFile(context), new String(data)); byte[] data = attr.getData();
db.attachment().setDownloaded(attachment.id, (long) data.length); Helper.writeText(attachment.getFile(context), new String(data));
db.attachment().setDownloaded(attachment.id, (long) data.length);
} catch (Throwable ex) {
Log.e(ex);
db.attachment().setError(attachment.id, Log.formatThrowable(ex));
}
} }
} else { } else {
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
@ -3643,30 +3653,35 @@ public class MessageHelper {
attachment.disposition = Part.ATTACHMENT; attachment.disposition = Part.ATTACHMENT;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
Helper.writeText(attachment.getFile(context), body); try {
db.attachment().setDownloaded(attachment.id, (long) body.length()); Helper.writeText(attachment.getFile(context), body);
db.attachment().setDownloaded(attachment.id, (long) body.length());
} catch (Throwable ex) {
Log.e(ex);
db.attachment().setError(attachment.id, Log.formatThrowable(ex));
}
} }
for (org.apache.poi.hmef.Attachment at : msg.getAttachments()) for (org.apache.poi.hmef.Attachment at : msg.getAttachments()) {
try { String filename = at.getLongFilename();
String filename = at.getLongFilename(); if (filename == null)
if (filename == null) filename = at.getFilename();
filename = at.getFilename(); if (filename == null) {
if (filename == null) { String ext = at.getExtension();
String ext = at.getExtension(); if (ext != null)
if (ext != null) filename = "document." + ext;
filename = "document." + ext; }
}
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
attachment.message = local.message; attachment.message = local.message;
attachment.sequence = local.sequence; attachment.sequence = local.sequence;
attachment.subsequence = ++subsequence; attachment.subsequence = ++subsequence;
attachment.name = filename; attachment.name = filename;
attachment.type = Helper.guessMimeType(attachment.name); attachment.type = Helper.guessMimeType(attachment.name);
attachment.disposition = Part.ATTACHMENT; attachment.disposition = Part.ATTACHMENT;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
try {
byte[] data = at.getContents(); byte[] data = at.getContents();
try (OutputStream os = new FileOutputStream(attachment.getFile(context))) { try (OutputStream os = new FileOutputStream(attachment.getFile(context))) {
os.write(data); os.write(data);
@ -3676,7 +3691,9 @@ public class MessageHelper {
} catch (Throwable ex) { } catch (Throwable ex) {
// java.lang.IllegalArgumentException: Attachment corrupt - no Data section // java.lang.IllegalArgumentException: Attachment corrupt - no Data section
Log.e(ex); Log.e(ex);
db.attachment().setError(attachment.id, Log.formatThrowable(ex));
} }
}
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (org.apache.poi.hmef.attribute.TNEFAttribute attr : msg.getMessageAttributes()) for (org.apache.poi.hmef.attribute.TNEFAttribute attr : msg.getMessageAttributes())
@ -3695,8 +3712,13 @@ public class MessageHelper {
attachment.disposition = Part.ATTACHMENT; attachment.disposition = Part.ATTACHMENT;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
Helper.writeText(attachment.getFile(context), sb.toString()); try {
db.attachment().setDownloaded(attachment.id, (long) sb.length()); Helper.writeText(attachment.getFile(context), sb.toString());
db.attachment().setDownloaded(attachment.id, (long) sb.length());
} catch (Throwable ex) {
Log.e(ex);
db.attachment().setError(attachment.id, Log.formatThrowable(ex));
}
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);

Loading…
Cancel
Save