diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index b8e60973f0..20efb614cc 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -142,6 +142,7 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac static final int REQUEST_IMPORT_CERTIFICATE = 7; static final int REQUEST_OAUTH = 8; static final int REQUEST_STILL = 9; + static final int REQUEST_DELETE_ACCOUNT = 10; static final int PI_MISC = 1; diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java b/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java index e63b5e8047..7fd872743d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java @@ -43,8 +43,12 @@ public class FragmentDialogSelectAccount extends FragmentDialogBase { new SimpleTask>() { @Override protected List onExecute(Context context, Bundle args) { + boolean all = (args != null && args.getBoolean("all")); + DB db = DB.getInstance(context); - return db.account().getSynchronizingAccounts(null); + return (all + ? db.account().getAccounts() + : db.account().getSynchronizingAccounts(null)); } @Override @@ -56,7 +60,7 @@ public class FragmentDialogSelectAccount extends FragmentDialogBase { protected void onException(Bundle args, Throwable ex) { Log.unexpectedError(getParentFragmentManager(), ex); } - }.execute(this, new Bundle(), "messages:accounts"); + }.execute(this, getArguments(), "messages:accounts"); return new AlertDialog.Builder(getContext()) .setIcon(R.drawable.twotone_account_circle_24) @@ -65,7 +69,9 @@ public class FragmentDialogSelectAccount extends FragmentDialogBase { @Override public void onClick(DialogInterface dialog, int which) { EntityAccount account = adapter.getItem(which); - getArguments().putLong("account", account.id); + Bundle args = getArguments(); + args.putLong("account", account.id); + args.putString("name", account.name); sendResult(RESULT_OK); } }) diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index c8dc7899b1..829318c1cb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -19,6 +19,8 @@ package eu.faircode.email; Copyright 2018-2022 by Marcel Bokhorst (M66B) */ +import static android.app.Activity.RESULT_OK; + import android.Manifest; import android.app.ActivityManager; import android.app.Dialog; @@ -110,6 +112,7 @@ public class FragmentSetup extends FragmentBase { private TextView tvSyncStopped; private Button btnApp; + private Button btnDelete; private Group grpInexactAlarms; private Group grpBackgroundRestricted; @@ -178,6 +181,7 @@ public class FragmentSetup extends FragmentBase { tvSyncStopped = view.findViewById(R.id.tvSyncStopped); btnApp = view.findViewById(R.id.btnApp); + btnDelete = view.findViewById(R.id.btnDelete); grpInexactAlarms = view.findViewById(R.id.grpInexactAlarms); grpBackgroundRestricted = view.findViewById(R.id.grpBackgroundRestricted); @@ -579,6 +583,19 @@ public class FragmentSetup extends FragmentBase { } }); + btnDelete.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Bundle args = new Bundle(); + args.putBoolean("all", true); + + FragmentDialogSelectAccount fragment = new FragmentDialogSelectAccount(); + fragment.setArguments(args); + fragment.setTargetFragment(FragmentSetup.this, ActivitySetup.REQUEST_DELETE_ACCOUNT); + fragment.show(getParentFragmentManager(), "setup:delete"); + } + }); + // Initialize FragmentDialogTheme.setBackground(getContext(), view, false); @@ -772,6 +789,22 @@ public class FragmentSetup extends FragmentBase { cardManual.setVisibility(manual ? View.VISIBLE : View.GONE); } + @Override + public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + try { + switch (requestCode) { + case ActivitySetup.REQUEST_DELETE_ACCOUNT: + if (resultCode == RESULT_OK && data != null) + onDeleteAccount(data.getBundleExtra("args")); + break; + } + } catch (Throwable ex) { + Log.e(ex); + } + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { for (int i = 0; i < permissions.length; i++) @@ -790,6 +823,48 @@ public class FragmentSetup extends FragmentBase { btnPermissions.setEnabled(!granted); } + private void onDeleteAccount(Bundle args) { + long account = args.getLong("account"); + String name = args.getString("name"); + + new AlertDialog.Builder(view.getContext()) + .setIcon(R.drawable.twotone_warning_24) + .setTitle(name) + .setMessage(R.string.title_account_delete) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Bundle args = new Bundle(); + args.putLong("id", account); + + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) throws Throwable { + long id = args.getLong("id"); + + DB db = DB.getInstance(context); + db.account().deleteAccount(id); + + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }.execute(FragmentSetup.this, args, "setup:delete"); + } + }) + .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Do nothing + } + }) + .show(); + + } + private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { diff --git a/app/src/main/res/layout/fragment_setup.xml b/app/src/main/res/layout/fragment_setup.xml index 5d0a9024ea..5e59917d87 100644 --- a/app/src/main/res/layout/fragment_setup.xml +++ b/app/src/main/res/layout/fragment_setup.xml @@ -875,18 +875,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvBatteryUsage" /> -