Use wakelock for mbox export

This commit is contained in:
M66B 2021-04-17 21:18:56 +02:00
parent eba065fb02
commit 9fb1a58036
1 changed files with 99 additions and 89 deletions

View File

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