From 7b6d61479fd7285a58efeab2cc0ac7bfa7c63bfa Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 3 Jun 2024 19:55:54 +0200 Subject: [PATCH] Dowload attachments: auto confirm --- .../FragmentDialogDownloadAttachments.java | 37 ++++++++++++++++++- .../layout/dialog_download_attachments.xml | 31 ++++++++++------ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogDownloadAttachments.java b/app/src/main/java/eu/faircode/email/FragmentDialogDownloadAttachments.java index 6aae456207..fcc2c567ac 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogDownloadAttachments.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogDownloadAttachments.java @@ -22,6 +22,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LiveData; import androidx.lifecycle.Observer; import androidx.preference.PreferenceManager; @@ -32,6 +33,8 @@ import java.util.List; public class FragmentDialogDownloadAttachments extends FragmentDialogBase { private List remaining; + private static final long AUTO_CONFIRM_DELAY = 2 * 1000L; + @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { @@ -51,12 +54,14 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase { NumberFormat NF = NumberFormat.getNumberInstance(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean attachments_auto_confirm = prefs.getBoolean("attachments_auto_confirm", false); View dview = LayoutInflater.from(context).inflate(R.layout.dialog_download_attachments, null); TextView tvRemark = dview.findViewById(R.id.tvRemark); Button btnDownload = dview.findViewById(R.id.btnDownload); ProgressBar pbDownloaded = dview.findViewById(R.id.pbDownloaded); TextView tvRemaining = dview.findViewById(R.id.tvRemaining); + CheckBox cbAutoConfirm = dview.findViewById(R.id.cbAutoConfirm); CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain); btnDownload.setOnClickListener(new View.OnClickListener() { @@ -96,7 +101,8 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase { long[] download = args.getLongArray("download"); DB db = DB.getInstance(context); - db.attachment().liveAttachments(id).observe(getViewLifecycleOwner(), new Observer>() { + LiveData> live = db.attachment().liveAttachments(id); + live.observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(List attachments) { if (attachments != null) @@ -112,6 +118,27 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase { tvRemaining.setText(getString(R.string.title_attachments_download, of)); updateButton(); + + if (remaining.isEmpty()) { + live.removeObserver(this); + + boolean attachments_auto_confirm = prefs.getBoolean("attachments_auto_confirm", false); + if (attachments_auto_confirm) + dview.postDelayed(new Runnable() { + @Override + public void run() { + try { + if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) + return; + startActivity(intent); + dismiss(); + } catch (Throwable ex) { + Log.e(ex); + } + } + }, AUTO_CONFIRM_DELAY); + + } } }); } @@ -124,6 +151,14 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase { } }); + cbAutoConfirm.setChecked(attachments_auto_confirm); + cbAutoConfirm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + prefs.edit().putBoolean("attachments_auto_confirm", isChecked).apply(); + } + }); + cbNotAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { diff --git a/app/src/main/res/layout/dialog_download_attachments.xml b/app/src/main/res/layout/dialog_download_attachments.xml index 203dfa72ef..8dca171276 100644 --- a/app/src/main/res/layout/dialog_download_attachments.xml +++ b/app/src/main/res/layout/dialog_download_attachments.xml @@ -66,16 +66,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/pbDownloaded" /> - - + app:layout_constraintTop_toBottomOf="@id/tvRemaining" /> + + + +