Consistently use database transactions

To prevent hard to find problem
pull/50/head
M66B 6 years ago
parent f0cf9dafa4
commit 92e9120e06

@ -168,54 +168,62 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
protected Long onLoad(Context context, Bundle args) throws Throwable {
File file = new File(context.getCacheDir(), "crash.log");
if (file.exists()) {
Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode");
// Get version info
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s: %s/%d\r\n", BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE));
sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
sb.append("\r\n");
// Get device info
sb.append(String.format("Brand: %s\r\n", Build.BRAND));
sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
sb.append(String.format("Model: %s\r\n", Build.MODEL));
sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
sb.append(String.format("Device: %s\r\n", Build.DEVICE));
sb.append(String.format("Host: %s\r\n", Build.HOST));
sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
sb.append(String.format("Id: %s\r\n", Build.ID));
sb.append("\r\n");
BufferedReader in = null;
try {
String line;
in = new BufferedReader(new FileReader(file));
while ((line = in.readLine()) != null)
sb.append(line);
} finally {
if (in != null)
in.close();
}
file.delete();
DB db = DB.getInstance(context);
EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts != null) {
Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode");
// Get version info
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s: %s/%d\r\n", BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE));
sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
sb.append("\r\n");
// Get device info
sb.append(String.format("Brand: %s\r\n", Build.BRAND));
sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
sb.append(String.format("Model: %s\r\n", Build.MODEL));
sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
sb.append(String.format("Device: %s\r\n", Build.DEVICE));
sb.append(String.format("Host: %s\r\n", Build.HOST));
sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
sb.append(String.format("Id: %s\r\n", Build.ID));
sb.append("\r\n");
BufferedReader in = null;
try {
String line;
in = new BufferedReader(new FileReader(file));
while ((line = in.readLine()) != null)
sb.append(line);
} finally {
if (in != null)
in.close();
try {
db.beginTransaction();
EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts != null) {
EntityMessage draft = new EntityMessage();
draft.account = drafts.account;
draft.folder = drafts.id;
draft.to = new Address[]{to};
draft.subject = context.getString(R.string.app_name) + " crash log";
draft.body = "<pre>" + sb.toString().replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
draft.id = db.message().insertMessage(draft);
return draft.id;
}
EntityMessage draft = new EntityMessage();
draft.account = drafts.account;
draft.folder = drafts.id;
draft.to = new Address[]{to};
draft.subject = context.getString(R.string.app_name) + " crash log";
draft.body = "<pre>" + sb.toString().replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
draft.id = db.message().insertMessage(draft);
file.delete();
return draft.id;
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@ -327,11 +335,21 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
@Override
protected Void onLoad(Context context, Bundle args) {
long time = args.getLong("time");
DaoAccount dao = DB.getInstance(context).account();
for (EntityAccount account : dao.getAccounts(true)) {
account.seen_until = time;
dao.updateAccount(account);
DB db = DB.getInstance(context);
try {
db.beginTransaction();
for (EntityAccount account : db.account().getAccounts(true)) {
account.seen_until = time;
db.account().updateAccount(account);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@ -449,30 +467,31 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
@Override
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
EntityFolder folder = db.folder().getFolder(message.folder);
if (!EntityFolder.OUTBOX.equals(folder.type) &&
!EntityFolder.ARCHIVE.equals(folder.type)) {
if (!message.seen && !message.ui_seen) {
try {
db.beginTransaction();
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
EntityFolder folder = db.folder().getFolder(message.folder);
if (!EntityFolder.OUTBOX.equals(folder.type) &&
!EntityFolder.ARCHIVE.equals(folder.type)) {
if (!message.seen && !message.ui_seen) {
message.ui_seen = !message.ui_seen;
db.message().updateMessage(message);
if (message.uid != null)
EntityOperation.queue(db, message, EntityOperation.SEEN, message.ui_seen);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
return null;
}

@ -199,12 +199,20 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
long message = args.getLong("message");
long sequence = args.getInt("sequence");
// No need for a transaction
DB db = DB.getInstance(context);
db.attachment().setProgress(id, 0);
try {
db.beginTransaction();
db.attachment().setProgress(id, 0);
EntityMessage msg = db.message().getMessage(message);
EntityOperation.queue(db, msg, EntityOperation.ATTACHMENT, sequence);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityMessage msg = db.message().getMessage(message);
EntityOperation.queue(db, msg, EntityOperation.ATTACHMENT, sequence);
EntityOperation.process(context);
return null;

@ -58,30 +58,37 @@ public class FragmentAbout extends FragmentEx {
new SimpleTask<Long>() {
@Override
protected Long onLoad(Context context, Bundle args) throws UnsupportedEncodingException {
DB db = DB.getInstance(context);
EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_drafts));
StringBuilder info = Helper.getDebugInfo();
info.insert(0, context.getString(R.string.title_debug_info_remark) + "\n\n\n\n");
Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode");
EntityMessage draft = new EntityMessage();
draft.account = drafts.account;
draft.folder = drafts.id;
draft.to = new Address[]{to};
draft.subject = BuildConfig.APPLICATION_ID + " debug info";
draft.body = "<pre>" + info.toString().replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
draft.id = db.message().insertMessage(draft);
return draft.id;
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_drafts));
EntityMessage draft = new EntityMessage();
draft.account = drafts.account;
draft.folder = drafts.id;
draft.to = new Address[]{to};
draft.subject = BuildConfig.APPLICATION_ID + " debug info";
draft.body = "<pre>" + info.toString().replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
draft.id = db.message().insertMessage(draft);
db.setTransactionSuccessful();
return draft.id;
} finally {
db.endTransaction();
}
}
@Override

@ -192,7 +192,6 @@ public class FragmentAccount extends FragmentEx {
throw new Throwable(getContext().getString(R.string.title_no_password));
// Check IMAP server / get folders
DB db = DB.getInstance(getContext());
List<EntityFolder> folders = new ArrayList<>();
Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null);
IMAPStore istore = null;
@ -234,6 +233,7 @@ public class FragmentAccount extends FragmentEx {
}
// Create entry
DB db = DB.getInstance(getContext());
EntityFolder folder = db.folder().getFolderByName(id, ifolder.getFullName());
if (folder == null) {
folder = new EntityFolder();
@ -416,26 +416,25 @@ public class FragmentAccount extends FragmentEx {
name = host + "/" + user;
DB db = DB.getInstance(getContext());
EntityAccount account = db.account().getAccount(args.getLong("id"));
boolean update = (account != null);
if (account == null)
account = new EntityAccount();
account.name = name;
account.host = host;
account.port = Integer.parseInt(port);
account.user = user;
account.password = password;
account.synchronize = synchronize;
account.primary = (account.synchronize && args.getBoolean("primary"));
// On disabling synchronization mark message seen until now
if (!account.synchronize && account.synchronize != synchronize)
account.seen_until = new Date().getTime();
try {
db.beginTransaction();
EntityAccount account = db.account().getAccount(args.getLong("id"));
boolean update = (account != null);
if (account == null)
account = new EntityAccount();
account.name = name;
account.host = host;
account.port = Integer.parseInt(port);
account.user = user;
account.password = password;
account.synchronize = synchronize;
account.primary = (account.synchronize && args.getBoolean("primary"));
// On disabling synchronization mark message seen until now
if (!account.synchronize && account.synchronize != synchronize)
account.seen_until = new Date().getTime();
if (account.primary)
db.account().resetPrimary();
@ -584,10 +583,8 @@ public class FragmentAccount extends FragmentEx {
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id", -1));
final DB db = DB.getInstance(getContext());
// Observe
db.account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
DB.getInstance(getContext()).account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
@Override
public void onChanged(@Nullable EntityAccount account) {
etName.setText(account == null ? null : account.name);

@ -374,28 +374,36 @@ public class FragmentCompose extends FragmentEx {
if (cursor == null || !cursor.moveToFirst())
return null;
String msgid = args.getString("msgid");
EntityAttachment attachment = new EntityAttachment();
DB db = DB.getInstance(context);
try {
db.beginTransaction();
String msgid = args.getString("msgid");
EntityMessage draft = db.message().getMessageByMsgId(msgid);
EntityMessage draft = db.message().getMessageByMsgId(msgid);
EntityAttachment attachment = new EntityAttachment();
attachment.message = draft.id;
attachment.sequence = db.attachment().getAttachmentCount(draft.id);
attachment.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
attachment.message = draft.id;
attachment.sequence = db.attachment().getAttachmentCount(draft.id);
attachment.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
String extension = MimeTypeMap.getFileExtensionFromUrl(attachment.name.toLowerCase());
if (extension != null)
attachment.type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (extension == null)
attachment.type = "application/octet-stream";
String extension = MimeTypeMap.getFileExtensionFromUrl(attachment.name.toLowerCase());
if (extension != null)
attachment.type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (extension == null)
attachment.type = "application/octet-stream";
String size = cursor.getString(cursor.getColumnIndex(OpenableColumns.SIZE));
String size = cursor.getString(cursor.getColumnIndex(OpenableColumns.SIZE));
attachment.size = (size == null ? null : Integer.parseInt(size));
attachment.progress = 0;
attachment.size = (size == null ? null : Integer.parseInt(size));
attachment.progress = 0;
attachment.id = db.attachment().insertAttachment(attachment);
attachment.id = db.attachment().insertAttachment(attachment);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
InputStream is = null;
try {
@ -472,92 +480,92 @@ public class FragmentCompose extends FragmentEx {
Log.i(Helper.TAG, "Load draft action=" + action + " id=" + id + " account=" + account + " reference=" + reference);
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityMessage draft = db.message().getMessage(id);
if (draft == null) {
if ("edit".equals(action))
throw new IllegalStateException("Message to edit not found");
try {
db.beginTransaction();
EntityMessage ref = db.message().getMessage(reference);
if (ref != null)
account = ref.account;
EntityFolder drafts;
drafts = db.folder().getFolderByType(account, EntityFolder.DRAFTS);
if (drafts == null)
drafts = db.folder().getPrimaryDrafts();
draft = new EntityMessage();
draft.account = account;
draft.folder = drafts.id;
draft.msgid = draft.generateMessageId();
if (ref != null) {
draft.thread = ref.thread;
if ("reply".equals(action)) {
draft.replying = ref.id;
draft.to = (ref.reply == null || ref.reply.length == 0 ? ref.from : ref.reply);
draft.from = ref.to;
} else if ("reply_all".equals(action)) {
draft.replying = ref.id;
List<Address> addresses = new ArrayList<>();
if (draft.reply != null && ref.reply.length > 0)
addresses.addAll(Arrays.asList(ref.reply));
else if (draft.from != null)
addresses.addAll(Arrays.asList(ref.from));
if (draft.cc != null)
addresses.addAll(Arrays.asList(ref.cc));
draft.to = addresses.toArray(new Address[0]);
draft.from = ref.to;
} else if ("forward".equals(action)) {
//msg.replying = ref.id;
draft.from = ref.to;
}
if ("reply".equals(action) || "reply_all".equals(action)) {
draft.subject = context.getString(R.string.title_subject_reply, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", draft.to)),
HtmlHelper.sanitize(context, ref.body, true));
} else if ("forward".equals(action)) {
draft.subject = context.getString(R.string.title_subject_forward, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", ref.from)),
HtmlHelper.sanitize(context, ref.body, true));
}
EntityMessage draft = db.message().getMessage(id);
if (draft == null) {
if ("edit".equals(action))
throw new IllegalStateException("Message to edit not found");
} else
return draft;
EntityMessage ref = db.message().getMessage(reference);
if (ref != null)
account = ref.account;
EntityFolder drafts;
drafts = db.folder().getFolderByType(account, EntityFolder.DRAFTS);
if (drafts == null)
drafts = db.folder().getPrimaryDrafts();
draft = new EntityMessage();
draft.account = account;
draft.folder = drafts.id;
draft.msgid = draft.generateMessageId();
if (ref != null) {
draft.thread = ref.thread;
if ("reply".equals(action)) {
draft.replying = ref.id;
draft.to = (ref.reply == null || ref.reply.length == 0 ? ref.from : ref.reply);
draft.from = ref.to;
} else if ("reply_all".equals(action)) {
draft.replying = ref.id;
List<Address> addresses = new ArrayList<>();
if (draft.reply != null && ref.reply.length > 0)
addresses.addAll(Arrays.asList(ref.reply));
else if (draft.from != null)
addresses.addAll(Arrays.asList(ref.from));
if (draft.cc != null)
addresses.addAll(Arrays.asList(ref.cc));
draft.to = addresses.toArray(new Address[0]);
draft.from = ref.to;
} else if ("forward".equals(action)) {
//msg.replying = ref.id;
draft.from = ref.to;
}
if ("new".equals(action))
draft.body = "";
if ("reply".equals(action) || "reply_all".equals(action)) {
draft.subject = context.getString(R.string.title_subject_reply, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", draft.to)),
HtmlHelper.sanitize(context, ref.body, true));
} else if ("forward".equals(action)) {
draft.subject = context.getString(R.string.title_subject_forward, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", ref.from)),
HtmlHelper.sanitize(context, ref.body, true));
}
}
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
if ("new".equals(action))
draft.body = "";
draft.id = db.message().insertMessage(draft);
draft.msgid = draft.generateMessageId();
db.message().updateMessage(draft);
args.putLong("id", draft.id);
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
EntityOperation.queue(db, draft, EntityOperation.ADD);
draft.id = db.message().insertMessage(draft);
draft.msgid = draft.generateMessageId();
db.message().updateMessage(draft);
args.putLong("id", draft.id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.queue(db, draft, EntityOperation.ADD);
EntityOperation.process(context);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
return draft;
}
@ -566,9 +574,7 @@ public class FragmentCompose extends FragmentEx {
FragmentCompose.this.draft = draft;
Log.i(Helper.TAG, "Loaded draft id=" + draft.id + " msgid=" + draft.msgid);
DB db = DB.getInstance(getContext());
db.message().liveMessageByMsgId(draft.msgid).observe(getViewLifecycleOwner(), new Observer<EntityMessage>() {
DB.getInstance(getContext()).message().liveMessageByMsgId(draft.msgid).observe(getViewLifecycleOwner(), new Observer<EntityMessage>() {
boolean observed = false;
@Override
@ -623,7 +629,7 @@ public class FragmentCompose extends FragmentEx {
bottom_navigation.getMenu().setGroupEnabled(0, true);
final DB db = DB.getInstance(getContext());
DB db = DB.getInstance(getContext());
db.identity().liveIdentities(true).removeObservers(getViewLifecycleOwner());
db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
@ -717,38 +723,38 @@ public class FragmentCompose extends FragmentEx {
// Get draft & selected identity
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessageByMsgId(id);
EntityIdentity identity = db.identity().getIdentity(iid);
// Draft deleted by server
// TODO: better handling of remote deleted message
if (draft == null)
throw new MessageRemovedException();
Log.i(Helper.TAG, "Load action msgid=" + draft.msgid + " action=" + action);
// Convert data
Address afrom[] = (identity == null ? null : new Address[]{new InternetAddress(identity.email, identity.name)});
Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to));
Address acc[] = (TextUtils.isEmpty(cc) ? null : InternetAddress.parse(cc));
Address abcc[] = (TextUtils.isEmpty(bcc) ? null : InternetAddress.parse(bcc));
// Update draft
draft.identity = (identity == null ? null : identity.id);
draft.from = afrom;
draft.to = ato;
draft.cc = acc;
draft.bcc = abcc;
draft.subject = subject;
draft.body = "<pre>" + body.replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime();
db.message().updateMessage(draft);
// Check data
try {
db.beginTransaction();
EntityMessage draft = db.message().getMessageByMsgId(id);
EntityIdentity identity = db.identity().getIdentity(iid);
// Draft deleted by server
// TODO: better handling of remote deleted message
if (draft == null)
throw new MessageRemovedException();
Log.i(Helper.TAG, "Load action id=" + draft.id + " msgid=" + draft.msgid + " action=" + action);
// Convert data
Address afrom[] = (identity == null ? null : new Address[]{new InternetAddress(identity.email, identity.name)});
Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to));
Address acc[] = (TextUtils.isEmpty(cc) ? null : InternetAddress.parse(cc));
Address abcc[] = (TextUtils.isEmpty(bcc) ? null : InternetAddress.parse(bcc));
// Update draft
draft.identity = (identity == null ? null : identity.id);
draft.from = afrom;
draft.to = ato;
draft.cc = acc;
draft.bcc = abcc;
draft.subject = subject;
draft.body = "<pre>" + body.replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime();
db.message().updateMessage(draft);
// Execute action
if (action == R.id.action_trash) {
draft.ui_hide = true;
db.message().updateMessage(draft);

@ -85,14 +85,21 @@ public class FragmentFolder extends FragmentEx {
int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
DB db = DB.getInstance(getContext());
DaoFolder dao = db.folder();
EntityFolder folder = dao.getFolder(id);
folder.synchronize = synchronize;
folder.after = days;
dao.updateFolder(folder);
if (!folder.synchronize)
db.message().deleteMessages(folder.id);
try {
db.beginTransaction();
EntityFolder folder = db.folder().getFolder(id);
folder.synchronize = synchronize;
folder.after = days;
db.folder().updateFolder(folder);
if (!folder.synchronize)
db.message().deleteMessages(folder.id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
} finally {

@ -42,7 +42,6 @@ import android.widget.Toast;
import com.google.android.material.textfield.TextInputLayout;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import javax.mail.Session;
@ -212,6 +211,7 @@ public class FragmentIdentity extends FragmentEx {
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
boolean synchronize = args.getBoolean("synchronize");
if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_name));
@ -231,38 +231,38 @@ public class FragmentIdentity extends FragmentEx {
if (TextUtils.isEmpty(replyto))
replyto = null;
DB db = DB.getInstance(getContext());
EntityIdentity identity = db.identity().getIdentity(id);
boolean update = (identity != null);
if (identity == null)
identity = new EntityIdentity();
identity.name = name;
identity.email = email;
identity.replyto = replyto;
identity.account = account;
identity.host = Objects.requireNonNull(host);
identity.port = Integer.parseInt(port);
identity.starttls = starttls;
identity.user = user;
identity.password = password;
identity.synchronize = args.getBoolean("synchronize");
identity.primary = (identity.synchronize && args.getBoolean("primary"));
// Check SMTP server
if (identity.synchronize) {
if (synchronize) {
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
Transport itransport = isession.getTransport(identity.starttls ? "smtp" : "smtps");
Transport itransport = isession.getTransport(starttls ? "smtp" : "smtps");
try {
itransport.connect(identity.host, identity.port, identity.user, identity.password);
itransport.connect(host, Integer.parseInt(port), user, password);
} finally {
itransport.close();
}
}
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
EntityIdentity identity = db.identity().getIdentity(id);
boolean update = (identity != null);
if (identity == null)
identity = new EntityIdentity();
identity.name = name;
identity.email = email;
identity.replyto = replyto;
identity.account = account;
identity.host = host;
identity.port = Integer.parseInt(port);
identity.starttls = starttls;
identity.user = user;
identity.password = password;
identity.synchronize = synchronize;
identity.primary = (identity.synchronize && args.getBoolean("primary"));
if (identity.primary)
db.identity().resetPrimary();

@ -433,7 +433,7 @@ public class FragmentMessage extends FragmentEx {
}.load(this, args);
}
private void onActionEdit(long id) {
private void onActionEdit(final long id) {
final MenuItem item = top_navigation.getMenu().findItem(R.id.action_edit);
item.setEnabled(false);
@ -443,22 +443,32 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Long>() {
new SimpleTask<Void>() {
@Override
protected Long onLoad(Context context, Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id);
EntityFolder drafts = db.folder().getFolderByType(draft.account, EntityFolder.DRAFTS);
draft.id = null;
draft.folder = drafts.id;
draft.uid = null;
draft.id = db.message().insertMessage(draft);
return id;
try {
db.beginTransaction();
EntityMessage draft = db.message().getMessage(id);
EntityFolder drafts = db.folder().getFolderByType(draft.account, EntityFolder.DRAFTS);
draft.id = null;
draft.folder = drafts.id;
draft.uid = null;
draft.id = db.message().insertMessage(draft);
db.setTransactionSuccessful();
return null;
} finally {
db.endTransaction();
}
}
@Override
protected void onLoaded(Bundle args, Long id) {
protected void onLoaded(Bundle args, Void data) {
item.setEnabled(true);
item.setIcon(icon);
getContext().startActivity(
@ -508,6 +518,7 @@ public class FragmentMessage extends FragmentEx {
@Override
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -570,6 +581,7 @@ public class FragmentMessage extends FragmentEx {
@Override
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -667,15 +679,26 @@ public class FragmentMessage extends FragmentEx {
new SimpleTask<List<EntityFolder>>() {
@Override
protected List<EntityFolder> onLoad(Context context, Bundle args) {
EntityMessage message;
List<EntityFolder> folders;
DB db = DB.getInstance(getContext());
EntityMessage message = db.message().getMessage(args.getLong("id"));
List<EntityFolder> folders = db.folder().getUserFolders(message.account);
try {
db.beginTransaction();
for (int i = 0; i < folders.size(); i++)
if (folders.get(i).id.equals(message.folder)) {
folders.remove(i);
break;
}
message = db.message().getMessage(args.getLong("id"));
folders = db.folder().getUserFolders(message.account);
for (int i = 0; i < folders.size(); i++)
if (folders.get(i).id.equals(message.folder)) {
folders.remove(i);
break;
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
@ -720,6 +743,7 @@ public class FragmentMessage extends FragmentEx {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
long target = args.getLong("target");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -777,6 +801,7 @@ public class FragmentMessage extends FragmentEx {
@Override
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
try {
db.beginTransaction();

@ -157,15 +157,24 @@ public class FragmentSetup extends FragmentEx {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
DB db = DB.getInstance(context);
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = new EntityFolder();
outbox.name = "OUTBOX";
outbox.type = EntityFolder.OUTBOX;
outbox.synchronize = false;
outbox.after = 0;
outbox.id = db.folder().insertFolder(outbox);
try {
db.beginTransaction();
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = new EntityFolder();
outbox.name = "OUTBOX";
outbox.type = EntityFolder.OUTBOX;
outbox.synchronize = false;
outbox.after = 0;
outbox.id = db.folder().insertFolder(outbox);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}

@ -34,6 +34,7 @@ import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
@ -175,18 +176,36 @@ public class ServiceSynchronize extends LifecycleService {
super.onStartCommand(intent, flags, startId);
if (intent != null && "unseen".equals(intent.getAction())) {
final long now = new Date().getTime();
executor.submit(new Runnable() {
Bundle args = new Bundle();
args.putLong("time", new Date().getTime());
new SimpleTask<Void>() {
@Override
public void run() {
DaoAccount dao = DB.getInstance(ServiceSynchronize.this).account();
for (EntityAccount account : dao.getAccounts(true)) {
account.seen_until = now;
dao.updateAccount(account);
protected Void onLoad(Context context, Bundle args) throws Throwable {
long time = args.getLong("time");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
for (EntityAccount account : db.account().getAccounts(true)) {
account.seen_until = time;
db.account().updateAccount(account);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
Log.i(Helper.TAG, "Updated seen until");
}
});
}.load(ServiceSynchronize.this, args);
}
return START_STICKY;
@ -362,11 +381,12 @@ public class ServiceSynchronize extends LifecycleService {
for (final EntityFolder folder : db.folder().getFolders(account.id, true)) {
Log.i(Helper.TAG, account.name + " sync folder " + folder.name);
// Monitor folders
new Thread(new Runnable() {
@Override
public void run() {
IMAPFolder ifolder = null;
DB db = DB.getInstance(ServiceSynchronize.this);
try {
Log.i(Helper.TAG, folder.name + " start");
@ -378,7 +398,7 @@ public class ServiceSynchronize extends LifecycleService {
}
folder.error = null;
db.folder().updateFolder(folder);
DB.getInstance(ServiceSynchronize.this).folder().updateFolder(folder);
monitorFolder(account, folder, fstore, ifolder, state);
@ -387,7 +407,7 @@ public class ServiceSynchronize extends LifecycleService {
reportError(account.name, folder.name, ex);
folder.error = Helper.formatThrowable(ex);
db.folder().updateFolder(folder);
DB.getInstance(ServiceSynchronize.this).folder().updateFolder(folder);
// Cascade up
if (!(ex instanceof FolderNotFoundException))
@ -410,11 +430,13 @@ public class ServiceSynchronize extends LifecycleService {
}, "sync.folder." + folder.id).start();
}
// Listen for folder operations
IntentFilter f = new IntentFilter(ACTION_PROCESS_OPERATIONS);
f.addDataType("account/" + account.id);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceSynchronize.this);
lbm.registerReceiver(processReceiver, f);
// Run folder operations
Log.i(Helper.TAG, "listen process folder");
for (final EntityFolder folder : db.folder().getFolders(account.id))
if (!EntityFolder.OUTBOX.equals(folder.type))
@ -610,9 +632,11 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, folder.name + " messages removed");
for (Message imessage : e.getMessages())
try {
DB db = DB.getInstance(ServiceSynchronize.this);
long uid = ifolder.getUID(imessage);
DB db = DB.getInstance(ServiceSynchronize.this);
int count = db.message().deleteMessage(folder.id, uid);
Log.i(Helper.TAG, "Deleted uid=" + uid + " count=" + count);
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
@ -993,10 +1017,10 @@ public class ServiceSynchronize extends LifecycleService {
try {
Log.i(Helper.TAG, "Start sync folders");
DaoFolder dao = DB.getInstance(this).folder();
DB db = DB.getInstance(this);
List<String> names = new ArrayList<>();
for (EntityFolder folder : dao.getUserFolders(account.id))
for (EntityFolder folder : db.folder().getUserFolders(account.id))
names.add(folder.name);
Log.i(Helper.TAG, "Local folder count=" + names.size());
@ -1020,7 +1044,7 @@ public class ServiceSynchronize extends LifecycleService {
if (selectable) {
Log.i(Helper.TAG, ifolder.getFullName() + " candidate attr=" + TextUtils.join(",", attrs));
EntityFolder folder = dao.getFolderByName(account.id, ifolder.getFullName());
EntityFolder folder = db.folder().getFolderByName(account.id, ifolder.getFullName());
if (folder == null) {
folder = new EntityFolder();
folder.account = account.id;
@ -1028,7 +1052,7 @@ public class ServiceSynchronize extends LifecycleService {
folder.type = EntityFolder.USER;
folder.synchronize = false;
folder.after = EntityFolder.DEFAULT_USER_SYNC;
dao.insertFolder(folder);
db.folder().insertFolder(folder);
Log.i(Helper.TAG, folder.name + " added");
} else
names.remove(folder.name);
@ -1037,7 +1061,7 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, "Delete local folder=" + names.size());
for (String name : names)
dao.deleteFolder(account.id, name);
db.folder().deleteFolder(account.id, name);
} finally {
Log.i(Helper.TAG, "End sync folder");
}
@ -1048,7 +1072,6 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, folder.name + " start sync after=" + folder.after);
DB db = DB.getInstance(this);
DaoMessage dao = db.message();
// Get reference times
Calendar cal = Calendar.getInstance();
@ -1062,11 +1085,11 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, folder.name + " ago=" + new Date(ago));
// Delete old local messages
int old = dao.deleteMessagesBefore(folder.id, ago);
int old = db.message().deleteMessagesBefore(folder.id, ago);
Log.i(Helper.TAG, folder.name + " local old=" + old);
// Get list of local uids
List<Long> uids = dao.getUids(folder.id, ago);
List<Long> uids = db.message().getUids(folder.id, ago);
Log.i(Helper.TAG, folder.name + " local count=" + uids.size());
// Reduce list of local uids
@ -1093,7 +1116,7 @@ public class ServiceSynchronize extends LifecycleService {
// Delete local messages not at remote
Log.i(Helper.TAG, folder.name + " delete=" + uids.size());
for (Long uid : uids) {
int count = dao.deleteMessage(folder.id, uid);
int count = db.message().deleteMessage(folder.id, uid);
Log.i(Helper.TAG, folder.name + " delete local uid=" + uid + " count=" + count);
}
@ -1102,27 +1125,19 @@ public class ServiceSynchronize extends LifecycleService {
int updated = 0;
int unchanged = 0;
Log.i(Helper.TAG, folder.name + " add=" + imessages.length);
for (int batch = 0; batch < imessages.length; batch += FETCH_BATCH_SIZE) {
Log.i(Helper.TAG, folder.name + " fetch @" + batch);
for (Message imessage : imessages)
try {
db.beginTransaction();
for (int i = 0; i < FETCH_BATCH_SIZE && batch + i < imessages.length; i++)
try {
int status = synchronizeMessage(folder, ifolder, (IMAPMessage) imessages[batch + i]);
if (status > 0)
added++;
else if (status < 0)
updated++;
else
unchanged++;
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
int status = synchronizeMessage(folder, ifolder, (IMAPMessage) imessage);
if (status > 0)
added++;
else if (status < 0)
updated++;
else
unchanged++;
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
}
}
Log.w(Helper.TAG, folder.name + " statistics added=" + added + " updated=" + updated + " unchanged=" + unchanged);
} finally {
Log.i(Helper.TAG, folder.name + " end sync");
@ -1292,7 +1307,7 @@ public class ServiceSynchronize extends LifecycleService {
try {
monitorAccount(account, state);
} catch (Throwable ex) {
// Fallsafe
// Fall-safe
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
}

@ -29,6 +29,7 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.OnLifecycleEvent;
//
@ -43,6 +44,10 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
run(context, owner, args);
}
public void load(LifecycleService service, Bundle args) {
run(service, service, args);
}
public void load(AppCompatActivity activity, Bundle args) {
run(activity, activity, args);
}

Loading…
Cancel
Save