From 511acdc875067f4bcadf3b01caad441c7a90473c Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 14 Aug 2018 06:40:36 +0000 Subject: [PATCH] Allow disabling account sync without internet connection --- .../java/eu/faircode/email/DaoFolder.java | 9 +-- .../eu/faircode/email/FragmentAccount.java | 59 ++++++++++--------- app/src/main/res/layout/fragment_account.xml | 5 +- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoFolder.java b/app/src/main/java/eu/faircode/email/DaoFolder.java index a2f30ccb42..f91622dcc8 100644 --- a/app/src/main/java/eu/faircode/email/DaoFolder.java +++ b/app/src/main/java/eu/faircode/email/DaoFolder.java @@ -95,15 +95,12 @@ public interface DaoFolder { @Query("UPDATE folder SET type = :type WHERE id = :id") int setFolderType(long id, String type); + @Query("UPDATE folder SET type = '" + EntityFolder.USER + "' WHERE type = :type") + int setFolderUser(String type); + @Query("UPDATE folder SET synchronize = :synchronize, after = :after WHERE id = :id") int setFolderProperties(long id, boolean synchronize, int after); @Query("DELETE FROM folder WHERE account= :account AND name = :name") void deleteFolder(Long account, String name); - - @Query("DELETE FROM folder" + - " WHERE account= :account" + - " AND type <> '" + EntityFolder.INBOX + "'" + - " AND type <> '" + EntityFolder.USER + "'") - int deleteSystemFolders(Long account); } diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index ea41a57a79..c4f168cdda 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -48,7 +48,6 @@ import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.Date; import java.util.List; import java.util.Locale; @@ -151,6 +150,8 @@ public class FragmentAccount extends FragmentEx { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { cbPrimary.setEnabled(checked); + btnCheck.setVisibility(checked ? View.VISIBLE : View.GONE); + btnSave.setVisibility(checked ? View.GONE : View.VISIBLE); } }); @@ -349,15 +350,15 @@ public class FragmentAccount extends FragmentEx { EntityFolder trash = (EntityFolder) spTrash.getSelectedItem(); EntityFolder junk = (EntityFolder) spJunk.getSelectedItem(); - if (drafts.type == null) + if (drafts != null && drafts.type == null) drafts = null; - if (sent.type == null) + if (sent != null && sent.type == null) sent = null; - if (all.type == null) + if (all != null && all.type == null) all = null; - if (trash.type == null) + if (trash != null && trash.type == null) trash = null; - if (junk.type == null) + if (junk != null && junk.type == null) junk = null; Bundle args = new Bundle(); @@ -398,29 +399,29 @@ public class FragmentAccount extends FragmentEx { throw new Throwable(getContext().getString(R.string.title_no_user)); if (TextUtils.isEmpty(password)) throw new Throwable(getContext().getString(R.string.title_no_password)); - if (drafts == null) + if (synchronize && drafts == null) throw new Throwable(getContext().getString(R.string.title_no_drafts)); // Check IMAP server - Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null); - IMAPStore istore = null; - try { - istore = (IMAPStore) isession.getStore("imaps"); - istore.connect(host, Integer.parseInt(port), user, password); + if (synchronize) { + Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null); + IMAPStore istore = null; + try { + istore = (IMAPStore) isession.getStore("imaps"); + istore.connect(host, Integer.parseInt(port), user, password); - if (!istore.hasCapability("IDLE")) - throw new MessagingException(getContext().getString(R.string.title_no_idle)); - } finally { - if (istore != null) - istore.close(); + if (!istore.hasCapability("IDLE")) + throw new MessagingException(getContext().getString(R.string.title_no_idle)); + } finally { + if (istore != null) + istore.close(); + } } if (TextUtils.isEmpty(name)) name = host + "/" + user; try { - ServiceSynchronize.stop(getContext(), "account"); - DB db = DB.getInstance(getContext()); try { db.beginTransaction(); @@ -437,10 +438,6 @@ public class FragmentAccount extends FragmentEx { 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(); @@ -459,8 +456,10 @@ public class FragmentAccount extends FragmentEx { folders.add(inbox); - drafts.type = EntityFolder.DRAFTS; - folders.add(drafts); + if (drafts != null) { + drafts.type = EntityFolder.DRAFTS; + folders.add(drafts); + } if (sent != null) { sent.type = EntityFolder.SENT; @@ -479,10 +478,8 @@ public class FragmentAccount extends FragmentEx { folders.add(junk); } - int count = db.folder().deleteSystemFolders(account.id); - Log.w(Helper.TAG, "Deleted system folders count=" + count); - for (EntityFolder folder : folders) { + db.folder().setFolderUser(folder.type); EntityFolder existing = db.folder().getFolderByName(account.id, folder.name); if (existing == null) { folder.account = account.id; @@ -591,6 +588,8 @@ public class FragmentAccount extends FragmentEx { DB.getInstance(getContext()).account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer() { @Override public void onChanged(@Nullable EntityAccount account) { + Helper.setViewsEnabled(view, true); + etName.setText(account == null ? null : account.name); etHost.setText(account == null ? null : account.host); etPort.setText(account == null ? null : Long.toString(account.port)); @@ -601,7 +600,9 @@ public class FragmentAccount extends FragmentEx { cbPrimary.setEnabled(account == null ? true : account.synchronize); ibDelete.setVisibility(account == null ? View.GONE : View.VISIBLE); - Helper.setViewsEnabled(view, true); + btnCheck.setVisibility(account.synchronize ? View.VISIBLE : View.GONE); + btnSave.setVisibility(account.synchronize ? View.GONE : View.VISIBLE); + btnCheck.setEnabled(true); pbWait.setVisibility(View.GONE); } diff --git a/app/src/main/res/layout/fragment_account.xml b/app/src/main/res/layout/fragment_account.xml index cc46a7daf2..e230b0c66c 100644 --- a/app/src/main/res/layout/fragment_account.xml +++ b/app/src/main/res/layout/fragment_account.xml @@ -202,9 +202,10 @@ android:id="@+id/ibDelete" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginTop="12dp" android:src="@drawable/baseline_delete_24" - app:layout_constraintBottom_toBottomOf="@id/btnCheck" - app:layout_constraintEnd_toEndOf="parent" /> + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toBottomOf="@id/cbPrimary" /> Password Synchronize (receive messages) Synchronize (send messages) - Primary (used to store drafts) + Primary (default account) Primary (default identity) Check Name missing