Use wakelock for mbox export

pull/197/head
M66B 4 years ago
parent eba065fb02
commit 9fb1a58036

@ -29,6 +29,7 @@ import android.graphics.Color;
import android.graphics.Rect; import android.graphics.Rect;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -870,99 +871,108 @@ public class FragmentFolders extends FragmentBase {
@Override @Override
protected Void onExecute(Context context, Bundle args) throws Throwable { protected Void onExecute(Context context, Bundle args) throws Throwable {
long fid = args.getLong("id"); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Uri uri = args.getParcelable("uri"); PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":mbox");
try {
wl.acquire();
if (!"content".equals(uri.getScheme())) { long fid = args.getLong("id");
Log.w("Export uri=" + uri); Uri uri = args.getParcelable("uri");
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
}
DB db = DB.getInstance(context); if (!"content".equals(uri.getScheme())) {
List<Long> ids = db.message().getMessageIdsByFolder(fid); Log.w("Export uri=" + uri);
if (ids == null) throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
return null; }
String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; DB db = DB.getInstance(context);
SimpleDateFormat df = new SimpleDateFormat(PATTERN_ASCTIME, Locale.US); List<Long> ids = db.message().getMessageIdsByFolder(fid);
if (ids == null)
return null;
Properties props = MessageHelper.getSessionProperties(); String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
Session isession = Session.getInstance(props, null); SimpleDateFormat df = new SimpleDateFormat(PATTERN_ASCTIME, Locale.US);
// https://www.ietf.org/rfc/rfc4155.txt (Appendix A) Properties props = MessageHelper.getSessionProperties();
// http://qmail.org./man/man5/mbox.html Session isession = Session.getInstance(props, null);
ContentResolver resolver = context.getContentResolver();
try (OutputStream out = new BufferedOutputStream(resolver.openOutputStream(uri))) {
for (long id : ids) {
EntityMessage message = db.message().getMessage(id);
if (message == null)
continue;
String email = null; // https://www.ietf.org/rfc/rfc4155.txt (Appendix A)
if (message.from != null && message.from.length > 0) // http://qmail.org./man/man5/mbox.html
email = ((InternetAddress) message.from[0]).getAddress(); ContentResolver resolver = context.getContentResolver();
if (TextUtils.isEmpty(email)) try (OutputStream out = new BufferedOutputStream(resolver.openOutputStream(uri))) {
email = "MAILER-DAEMON"; for (long id : ids) {
EntityMessage message = db.message().getMessage(id);
if (message == null)
continue;
out.write(("From " + email + " " + df.format(message.received) + "\n").getBytes()); String email = null;
if (message.from != null && message.from.length > 0)
email = ((InternetAddress) message.from[0]).getAddress();
if (TextUtils.isEmpty(email))
email = "MAILER-DAEMON";
Message imessage = MessageHelper.from(context, message, null, isession, false); out.write(("From " + email + " " + df.format(message.received) + "\n").getBytes());
imessage.writeTo(new FilterOutputStream(out) {
private boolean cr = false;
private ByteArrayOutputStream buffer = new ByteArrayOutputStream(998);
@Override Message imessage = MessageHelper.from(context, message, null, isession, false);
public void write(int b) throws IOException { imessage.writeTo(new FilterOutputStream(out) {
if (b == 13 /* CR */) { private boolean cr = false;
if (cr) // another private ByteArrayOutputStream buffer = new ByteArrayOutputStream(998);
line();
cr = true; @Override
} else if (b == 10 /* LF */) { public void write(int b) throws IOException {
line(); if (b == 13 /* CR */) {
} else { if (cr) // another
if (cr) // dangling line();
cr = true;
} else if (b == 10 /* LF */) {
line(); line();
buffer.write(b); } else {
if (cr) // dangling
line();
buffer.write(b);
}
} }
}
@Override @Override
public void flush() throws IOException { public void flush() throws IOException {
if (buffer.size() > 0 || cr /* dangling */) if (buffer.size() > 0 || cr /* dangling */)
line(); line();
out.write(10); out.write(10);
super.flush(); super.flush();
} }
private void line() throws IOException { private void line() throws IOException {
byte[] b = buffer.toByteArray(); byte[] b = buffer.toByteArray();
int i = 0; int i = 0;
for (; i < b.length; i++) for (; i < b.length; i++)
if (b[i] != '>') if (b[i] != '>')
break; break;
if (i + 4 < b.length && if (i + 4 < b.length &&
b[i + 0] == 'F' && b[i + 0] == 'F' &&
b[i + 1] == 'r' && b[i + 1] == 'r' &&
b[i + 2] == 'o' && b[i + 2] == 'o' &&
b[i + 3] == 'm' && b[i + 3] == 'm' &&
b[i + 4] == ' ') b[i + 4] == ' ')
out.write('>'); out.write('>');
for (i = 0; i < b.length; i++) for (i = 0; i < b.length; i++)
out.write(b[i]); out.write(b[i]);
out.write(10); out.write(10);
buffer.reset(); buffer.reset();
cr = false; cr = false;
} }
}); });
}
} }
}
return null; return null;
} finally {
wl.release();
}
} }
@Override @Override

Loading…
Cancel
Save