Allow disabling account sync without internet connection

pull/50/head
M66B 7 years ago
parent 81255dc31e
commit 511acdc875

@ -95,15 +95,12 @@ public interface DaoFolder {
@Query("UPDATE folder SET type = :type WHERE id = :id") @Query("UPDATE folder SET type = :type WHERE id = :id")
int setFolderType(long id, String type); 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") @Query("UPDATE folder SET synchronize = :synchronize, after = :after WHERE id = :id")
int setFolderProperties(long id, boolean synchronize, int after); int setFolderProperties(long id, boolean synchronize, int after);
@Query("DELETE FROM folder WHERE account= :account AND name = :name") @Query("DELETE FROM folder WHERE account= :account AND name = :name")
void deleteFolder(Long account, String 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);
} }

@ -48,7 +48,6 @@ import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -151,6 +150,8 @@ public class FragmentAccount extends FragmentEx {
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
cbPrimary.setEnabled(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 trash = (EntityFolder) spTrash.getSelectedItem();
EntityFolder junk = (EntityFolder) spJunk.getSelectedItem(); EntityFolder junk = (EntityFolder) spJunk.getSelectedItem();
if (drafts.type == null) if (drafts != null && drafts.type == null)
drafts = null; drafts = null;
if (sent.type == null) if (sent != null && sent.type == null)
sent = null; sent = null;
if (all.type == null) if (all != null && all.type == null)
all = null; all = null;
if (trash.type == null) if (trash != null && trash.type == null)
trash = null; trash = null;
if (junk.type == null) if (junk != null && junk.type == null)
junk = null; junk = null;
Bundle args = new Bundle(); Bundle args = new Bundle();
@ -398,10 +399,11 @@ public class FragmentAccount extends FragmentEx {
throw new Throwable(getContext().getString(R.string.title_no_user)); throw new Throwable(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password)) if (TextUtils.isEmpty(password))
throw new Throwable(getContext().getString(R.string.title_no_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)); throw new Throwable(getContext().getString(R.string.title_no_drafts));
// Check IMAP server // Check IMAP server
if (synchronize) {
Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null); Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null);
IMAPStore istore = null; IMAPStore istore = null;
try { try {
@ -414,13 +416,12 @@ public class FragmentAccount extends FragmentEx {
if (istore != null) if (istore != null)
istore.close(); istore.close();
} }
}
if (TextUtils.isEmpty(name)) if (TextUtils.isEmpty(name))
name = host + "/" + user; name = host + "/" + user;
try { try {
ServiceSynchronize.stop(getContext(), "account");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
try { try {
db.beginTransaction(); db.beginTransaction();
@ -437,10 +438,6 @@ public class FragmentAccount extends FragmentEx {
account.synchronize = synchronize; account.synchronize = synchronize;
account.primary = (account.synchronize && args.getBoolean("primary")); 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) if (account.primary)
db.account().resetPrimary(); db.account().resetPrimary();
@ -459,8 +456,10 @@ public class FragmentAccount extends FragmentEx {
folders.add(inbox); folders.add(inbox);
if (drafts != null) {
drafts.type = EntityFolder.DRAFTS; drafts.type = EntityFolder.DRAFTS;
folders.add(drafts); folders.add(drafts);
}
if (sent != null) { if (sent != null) {
sent.type = EntityFolder.SENT; sent.type = EntityFolder.SENT;
@ -479,10 +478,8 @@ public class FragmentAccount extends FragmentEx {
folders.add(junk); folders.add(junk);
} }
int count = db.folder().deleteSystemFolders(account.id);
Log.w(Helper.TAG, "Deleted system folders count=" + count);
for (EntityFolder folder : folders) { for (EntityFolder folder : folders) {
db.folder().setFolderUser(folder.type);
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name); EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) { if (existing == null) {
folder.account = account.id; folder.account = account.id;
@ -591,6 +588,8 @@ public class FragmentAccount extends FragmentEx {
DB.getInstance(getContext()).account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() { DB.getInstance(getContext()).account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
@Override @Override
public void onChanged(@Nullable EntityAccount account) { public void onChanged(@Nullable EntityAccount account) {
Helper.setViewsEnabled(view, true);
etName.setText(account == null ? null : account.name); etName.setText(account == null ? null : account.name);
etHost.setText(account == null ? null : account.host); etHost.setText(account == null ? null : account.host);
etPort.setText(account == null ? null : Long.toString(account.port)); 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); cbPrimary.setEnabled(account == null ? true : account.synchronize);
ibDelete.setVisibility(account == null ? View.GONE : View.VISIBLE); 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); btnCheck.setEnabled(true);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
} }

@ -202,9 +202,10 @@
android:id="@+id/ibDelete" android:id="@+id/ibDelete"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:src="@drawable/baseline_delete_24" 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" />
<TextView <TextView
android:id="@+id/tvDrafts" android:id="@+id/tvDrafts"

@ -79,7 +79,7 @@
<string name="title_password">Password</string> <string name="title_password">Password</string>
<string name="title_synchronize_account">Synchronize (receive messages)</string> <string name="title_synchronize_account">Synchronize (receive messages)</string>
<string name="title_synchronize_identity">Synchronize (send messages)</string> <string name="title_synchronize_identity">Synchronize (send messages)</string>
<string name="title_primary_account">Primary (used to store drafts)</string> <string name="title_primary_account">Primary (default account)</string>
<string name="title_primary_identity">Primary (default identity)</string> <string name="title_primary_identity">Primary (default identity)</string>
<string name="title_check">Check</string> <string name="title_check">Check</string>
<string name="title_no_name">Name missing</string> <string name="title_no_name">Name missing</string>

Loading…
Cancel
Save