From 707e4f7167b537804b6abf9ae11dbe5a128b3bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Szcz=C4=99k?= Date: Fri, 11 Oct 2019 15:07:51 +0200 Subject: [PATCH 1/9] Add option to remove downloaded files when clearing finished downloads --- .../shandian/giga/ui/adapter/MissionAdapter.java | 12 +++++++++++- .../giga/ui/fragment/MissionsFragment.java | 8 +++++++- app/src/main/res/layout/delete_files_checkbox.xml | 14 ++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/layout/delete_files_checkbox.xml diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index 8420e343b..e20417aa9 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -19,6 +19,7 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.webkit.MimeTypeMap; +import android.widget.CheckBox; import android.widget.ImageView; import android.widget.PopupMenu; import android.widget.TextView; @@ -557,7 +558,16 @@ public class MissionAdapter extends Adapter implements Handler.Callb ); } - public void clearFinishedDownloads() { + public void clearFinishedDownloads(boolean delete) { + if (delete && mIterator.hasFinishedMissions()) { + for(int i=0; i mAdapter.clearFinishedDownloads()); + prompt.setView(checkBoxView); + prompt.setPositiveButton(android.R.string.ok, (dialog, which) -> { + CheckBox checkBox = checkBoxView.findViewById(R.id.delete_files_checkbox); + mAdapter.clearFinishedDownloads(checkBox.isChecked()); + }); prompt.setNegativeButton(R.string.cancel, null); prompt.create().show(); return true; diff --git a/app/src/main/res/layout/delete_files_checkbox.xml b/app/src/main/res/layout/delete_files_checkbox.xml new file mode 100644 index 000000000..cbc6ecd10 --- /dev/null +++ b/app/src/main/res/layout/delete_files_checkbox.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 54ae40d1d..d3899583b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -587,5 +587,5 @@ Use SAF The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible Choose an instance - + Delete downloaded files From f6bbc69cf9a8db92aafb4c20bde999f3c90598c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Szcz=C4=99k?= Date: Fri, 11 Oct 2019 16:16:10 +0200 Subject: [PATCH 2/9] Remove unnecessary import --- .../main/java/us/shandian/giga/ui/adapter/MissionAdapter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index e20417aa9..ab3f22886 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -19,7 +19,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.webkit.MimeTypeMap; -import android.widget.CheckBox; import android.widget.ImageView; import android.widget.PopupMenu; import android.widget.TextView; From b18236a27e05076225f2b151560511d6279e36ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Szcz=C4=99k?= Date: Fri, 11 Oct 2019 16:36:47 +0200 Subject: [PATCH 3/9] Put call to forget finished downloads in an else statement to prevent potential bugs --- .../main/java/us/shandian/giga/ui/adapter/MissionAdapter.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index ab3f22886..4842e6b2e 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -566,8 +566,9 @@ public class MissionAdapter extends Adapter implements Handler.Callb mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri())); } } + } else if (!delete) { + mDownloadManager.forgetFinishedDownloads(); } - mDownloadManager.forgetFinishedDownloads(); applyChanges(); } From cfad3fb5ded10334317501fde5b18cf7859c6746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Szcz=C4=99k?= Date: Wed, 11 Dec 2019 15:56:04 +0100 Subject: [PATCH 4/9] Fix inconsistent style --- .../main/java/us/shandian/giga/ui/adapter/MissionAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index 4842e6b2e..eea3f2795 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -559,7 +559,7 @@ public class MissionAdapter extends Adapter implements Handler.Callb public void clearFinishedDownloads(boolean delete) { if (delete && mIterator.hasFinishedMissions()) { - for(int i=0; i Date: Fri, 13 Dec 2019 11:48:01 +0100 Subject: [PATCH 5/9] Use a dialog option instead of a checkbox --- .../giga/ui/fragment/MissionsFragment.java | 8 ++------ app/src/main/res/layout/delete_files_checkbox.xml | 14 -------------- app/src/main/res/values/strings.xml | 2 +- 3 files changed, 3 insertions(+), 21 deletions(-) delete mode 100644 app/src/main/res/layout/delete_files_checkbox.xml diff --git a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java index edaa01e0f..98231cffe 100644 --- a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java +++ b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java @@ -190,14 +190,10 @@ public class MissionsFragment extends Fragment { return true; case R.id.clear_list: AlertDialog.Builder prompt = new AlertDialog.Builder(mContext); - View checkBoxView = View.inflate(this.getContext(), R.layout.delete_files_checkbox, null); prompt.setTitle(R.string.clear_finished_download); prompt.setMessage(R.string.confirm_prompt); - prompt.setView(checkBoxView); - prompt.setPositiveButton(android.R.string.ok, (dialog, which) -> { - CheckBox checkBox = checkBoxView.findViewById(R.id.delete_files_checkbox); - mAdapter.clearFinishedDownloads(checkBox.isChecked()); - }); + prompt.setPositiveButton(R.string.clear_finished_download, (dialog, which) -> mAdapter.clearFinishedDownloads(false)); + prompt.setNeutralButton(R.string.delete_downloaded_files, (dialog, which) -> mAdapter.clearFinishedDownloads(true)); prompt.setNegativeButton(R.string.cancel, null); prompt.create().show(); return true; diff --git a/app/src/main/res/layout/delete_files_checkbox.xml b/app/src/main/res/layout/delete_files_checkbox.xml deleted file mode 100644 index cbc6ecd10..000000000 --- a/app/src/main/res/layout/delete_files_checkbox.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d3899583b..94a50972f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -570,7 +570,7 @@ Connection timeout Cannot recover this download Clear finished downloads - Are you sure? + Do you want to clear your download history or delete all downloaded files? Stop Maximum retries Maximum number of attempts before canceling the download From 986acc5fc5962e67fd85f4034c4118932da136f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Szcz=C4=99k?= Date: Thu, 26 Dec 2019 16:15:30 +0100 Subject: [PATCH 6/9] Reorder buttons in clear downloads dialog --- .../us/shandian/giga/ui/fragment/MissionsFragment.java | 9 +++++---- app/src/main/res/menu/download_menu.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java index 98231cffe..be0b0bb25 100644 --- a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java +++ b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java @@ -190,11 +190,12 @@ public class MissionsFragment extends Fragment { return true; case R.id.clear_list: AlertDialog.Builder prompt = new AlertDialog.Builder(mContext); - prompt.setTitle(R.string.clear_finished_download); + prompt.setTitle(R.string.clear_download_history); prompt.setMessage(R.string.confirm_prompt); - prompt.setPositiveButton(R.string.clear_finished_download, (dialog, which) -> mAdapter.clearFinishedDownloads(false)); - prompt.setNeutralButton(R.string.delete_downloaded_files, (dialog, which) -> mAdapter.clearFinishedDownloads(true)); - prompt.setNegativeButton(R.string.cancel, null); + // Intentionally misusing button's purpose in order to achieve good order + prompt.setNegativeButton(R.string.clear_download_history, (dialog, which) -> mAdapter.clearFinishedDownloads(false)); + prompt.setPositiveButton(R.string.delete_downloaded_files, (dialog, which) -> mAdapter.clearFinishedDownloads(true)); + prompt.setNeutralButton(R.string.cancel, null); prompt.create().show(); return true; case R.id.start_downloads: diff --git a/app/src/main/res/menu/download_menu.xml b/app/src/main/res/menu/download_menu.xml index f91f8ad7b..8728e146e 100644 --- a/app/src/main/res/menu/download_menu.xml +++ b/app/src/main/res/menu/download_menu.xml @@ -24,7 +24,7 @@ Progress lost, because the file was deleted Connection timeout Cannot recover this download - Clear finished downloads + Clear download history Do you want to clear your download history or delete all downloaded files? Stop Maximum retries From d071891b2af8b71f428d2a16815d783f16fca089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Szcz=C4=99k?= Date: Sun, 5 Jan 2020 14:01:54 +0100 Subject: [PATCH 7/9] Add a snackbar to allow user to undo file deletion --- .../giga/ui/adapter/MissionAdapter.java | 52 +++++++++++++++++-- app/src/main/res/values/strings.xml | 1 + 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index eea3f2795..11cdf5a3d 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -5,6 +5,7 @@ import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; +import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; @@ -35,6 +36,8 @@ import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView.Adapter; import androidx.recyclerview.widget.RecyclerView.ViewHolder; +import com.google.android.material.snackbar.Snackbar; + import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.NewPipe; @@ -46,6 +49,7 @@ import java.io.File; import java.lang.ref.WeakReference; import java.net.URI; import java.util.ArrayList; +import java.util.Iterator; import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.FinishedMission; @@ -84,6 +88,7 @@ public class MissionAdapter extends Adapter implements Handler.Callb private static final String UNDEFINED_PROGRESS = "--.-%"; private static final String DEFAULT_MIME_TYPE = "*/*"; private static final String UNDEFINED_ETA = "--:--"; + private static final int TIMEOUT = 5000;// ms static { @@ -104,8 +109,12 @@ public class MissionAdapter extends Adapter implements Handler.Callb private MenuItem mPauseButton; private View mEmptyMessage; private RecoverHelper mRecover; + private View mView; + private ArrayList mHidden; + private Snackbar mSnackbar; private final Runnable rUpdater = this::updater; + private final Runnable rDelete = this::deleteFinishedDownloads; public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage, View root) { mContext = context; @@ -122,6 +131,10 @@ public class MissionAdapter extends Adapter implements Handler.Callb mDeleter = new Deleter(root, mContext, this, mDownloadManager, mIterator, mHandler); + mView = root; + + mHidden = new ArrayList<>(); + checkEmptyMessageVisibility(); onResume(); } @@ -558,18 +571,49 @@ public class MissionAdapter extends Adapter implements Handler.Callb } public void clearFinishedDownloads(boolean delete) { - if (delete && mIterator.hasFinishedMissions()) { + if (delete && mIterator.hasFinishedMissions() && mHidden.isEmpty()) { for (int i = 0; i < mIterator.getOldListSize(); i++) { FinishedMission mission = mIterator.getItem(i).mission instanceof FinishedMission ? (FinishedMission) mIterator.getItem(i).mission : null; if (mission != null) { - mDownloadManager.deleteMission(mission); - mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri())); + mIterator.hide(mission); + mHidden.add(mission); } } + applyChanges(); + + String msg = String.format(mContext.getString(R.string.deleted_downloads), String.valueOf(mHidden.size())); + mSnackbar = Snackbar.make(mView, msg, Snackbar.LENGTH_INDEFINITE); + mSnackbar.setAction(R.string.undo, s -> { + Iterator i = mHidden.iterator(); + while (i.hasNext()) { + mIterator.unHide(i.next()); + i.remove(); + } + applyChanges(); + mHandler.removeCallbacks(rDelete); + }); + mSnackbar.setActionTextColor(Color.YELLOW); + mSnackbar.show(); + + mHandler.postDelayed(rDelete, TIMEOUT); } else if (!delete) { mDownloadManager.forgetFinishedDownloads(); + applyChanges(); + } + } + + private void deleteFinishedDownloads() { + if(mSnackbar != null) mSnackbar.dismiss(); + + Iterator i = mHidden.iterator(); + while (i.hasNext()) { + Mission mission = i.next(); + if (mission != null) { + mDownloadManager.deleteMission(mission); + mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri())); + } + i.remove(); } - applyChanges(); } private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem option) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bc3389062..9e3f89357 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -588,4 +588,5 @@ The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible Choose an instance Delete downloaded files + Deleted %s downloads From 2c8222fd559d4d536b4f2f35cf1f0b6ed87223ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Szcz=C4=99k?= Date: Sat, 11 Jan 2020 15:19:24 +0100 Subject: [PATCH 8/9] Style corrected --- .../java/us/shandian/giga/ui/adapter/MissionAdapter.java | 5 ++--- app/src/main/res/values/strings.xml | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index 11cdf5a3d..fac196e0b 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -88,7 +88,6 @@ public class MissionAdapter extends Adapter implements Handler.Callb private static final String UNDEFINED_PROGRESS = "--.-%"; private static final String DEFAULT_MIME_TYPE = "*/*"; private static final String UNDEFINED_ETA = "--:--"; - private static final int TIMEOUT = 5000;// ms static { @@ -595,7 +594,7 @@ public class MissionAdapter extends Adapter implements Handler.Callb mSnackbar.setActionTextColor(Color.YELLOW); mSnackbar.show(); - mHandler.postDelayed(rDelete, TIMEOUT); + mHandler.postDelayed(rDelete, 5000); } else if (!delete) { mDownloadManager.forgetFinishedDownloads(); applyChanges(); @@ -603,7 +602,7 @@ public class MissionAdapter extends Adapter implements Handler.Callb } private void deleteFinishedDownloads() { - if(mSnackbar != null) mSnackbar.dismiss(); + if (mSnackbar != null) mSnackbar.dismiss(); Iterator i = mHidden.iterator(); while (i.hasNext()) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9e3f89357..431f05d5a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -571,6 +571,8 @@ Cannot recover this download Clear download history Do you want to clear your download history or delete all downloaded files? + Delete downloaded files + Deleted %s downloads Stop Maximum retries Maximum number of attempts before canceling the download @@ -587,6 +589,4 @@ Use SAF The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible Choose an instance - Delete downloaded files - Deleted %s downloads From 3d93ecd6ec35467191178e35dc64ca8a7716e41d Mon Sep 17 00:00:00 2001 From: TobiGr Date: Mon, 13 Jan 2020 20:25:32 +0100 Subject: [PATCH 9/9] Use Integer value directly for formatted string a --- .../main/java/us/shandian/giga/ui/adapter/MissionAdapter.java | 2 +- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index fac196e0b..aaf7826ef 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -580,7 +580,7 @@ public class MissionAdapter extends Adapter implements Handler.Callb } applyChanges(); - String msg = String.format(mContext.getString(R.string.deleted_downloads), String.valueOf(mHidden.size())); + String msg = String.format(mContext.getString(R.string.deleted_downloads), mHidden.size()); mSnackbar = Snackbar.make(mView, msg, Snackbar.LENGTH_INDEFINITE); mSnackbar.setAction(R.string.undo, s -> { Iterator i = mHidden.iterator(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 431f05d5a..e1b2cc0a4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -572,7 +572,7 @@ Clear download history Do you want to clear your download history or delete all downloaded files? Delete downloaded files - Deleted %s downloads + Deleted %1$s downloads Stop Maximum retries Maximum number of attempts before canceling the download