From ab6e8ed6dd481d6455f8d45be0bb012a91e0e8f4 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 11 Aug 2018 18:16:35 +0000 Subject: [PATCH] Replace executors by simple loader --- .../java/eu/faircode/email/ActivitySetup.java | 2 ++ .../eu/faircode/email/FragmentAccount.java | 34 +++++++++---------- .../eu/faircode/email/FragmentIdentity.java | 22 ++++++------ 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index 2e0cfa85..184c6431 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -32,6 +32,8 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager; public class ActivitySetup extends ActivityBase implements FragmentManager.OnBackStackChangedListener { static final int LOADER_CREATE_OUTBOX = 1; + static final int LOADER_DELETE_ACCOUNT = 2; + static final int LOADER_DELETE_IDENTITY = 3; static final String ACTION_EDIT_ACCOUNT = BuildConfig.APPLICATION_ID + ".EDIT_ACCOUNT"; static final String ACTION_EDIT_IDENTITY = BuildConfig.APPLICATION_ID + ".EDIT_IDENTITY"; diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index f42fe2c7..f5651e11 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -51,8 +51,6 @@ import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Locale; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import javax.mail.Folder; import javax.mail.MessagingException; @@ -92,8 +90,6 @@ public class FragmentAccount extends FragmentEx { private ProgressBar pbWait; private Group grpFolders; - private ExecutorService executor = Executors.newCachedThreadPool(); - @Override @Nullable public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -240,20 +236,23 @@ public class FragmentAccount extends FragmentEx { @Override public void onClick(DialogInterface dialog, int which) { getFragmentManager().popBackStack(); + // TODO: spinner - executor.submit(new Runnable() { + new SimpleLoader() { + @Override + public Void onLoad(Bundle args) { + // To prevent foreign key constraints from triggering + ServiceSynchronize.stop(getContext(), "delete account"); + DB.getInstance(getContext()).account().deleteAccount(id); + ServiceSynchronize.start(getContext()); + return null; + } + @Override - public void run() { - try { - // To prevent foreign key constraints from triggering - ServiceSynchronize.stop(getContext(), "delete account"); - DB.getInstance(getContext()).account().deleteAccount(id); - ServiceSynchronize.start(getContext()); - } catch (Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } + public void onException(Bundle args, Throwable ex) { + Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); } - }); + }.load(FragmentAccount.this, ActivitySetup.LOADER_DELETE_ACCOUNT, new Bundle()); } }) .setNegativeButton(android.R.string.cancel, null).show(); @@ -436,8 +435,9 @@ public class FragmentAccount extends FragmentEx { Collections.sort(data.folders, new Comparator() { @Override public int compare(EntityFolder f1, EntityFolder f2) { - int s = ((Integer) EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type)) - .compareTo(EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type)); + int s = Integer.compare( + EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type), + EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type)); if (s != 0) return s; int c = -f1.synchronize.compareTo(f2.synchronize); diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index d631e92a..33cf13c1 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -45,8 +45,6 @@ import com.google.android.material.textfield.TextInputLayout; import java.util.List; import java.util.Objects; import java.util.Properties; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import javax.mail.Session; import javax.mail.Transport; @@ -80,8 +78,6 @@ public class FragmentIdentity extends FragmentEx { private ImageButton ibDelete; private ProgressBar pbWait; - private ExecutorService executor = Executors.newCachedThreadPool(); - @Override @Nullable public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -220,16 +216,18 @@ public class FragmentIdentity extends FragmentEx { public void onClick(DialogInterface dialog, int which) { getFragmentManager().popBackStack(); // TODO: spinner - executor.submit(new Runnable() { + new SimpleLoader() { + @Override + public Void onLoad(Bundle args) throws Throwable { + DB.getInstance(getContext()).identity().deleteIdentity(id); + return null; + } + @Override - public void run() { - try { - DB.getInstance(getContext()).identity().deleteIdentity(id); - } catch (Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } + public void onException(Bundle args, Throwable ex) { + Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); } - }); + }.load(FragmentIdentity.this, ActivitySetup.LOADER_DELETE_IDENTITY, new Bundle()); } }) .setNegativeButton(android.R.string.cancel, null).show();