diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 72dc2fdde..d9a897d73 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -757,15 +757,14 @@ public class DownloadDialog extends DialogFragment return; } - // check if is our file + // get state of potential mission referring to the same file final MissionState state = downloadManager.checkForExistingMission(storage); - @StringRes - final int msgBtn; - @StringRes - final int msgBody; + @StringRes final int msgBtn; + @StringRes final int msgBody; + // this switch checks if there is already a mission referring to the same file switch (state) { - case Finished: + case Finished: // there is already a finished mission msgBtn = R.string.overwrite; msgBody = R.string.overwrite_finished_warning; break; @@ -777,7 +776,7 @@ public class DownloadDialog extends DialogFragment msgBtn = R.string.generate_unique_name; msgBody = R.string.download_already_running; break; - case None: + case None: // there is no mission referring to the same file if (mainStorage == null) { // This part is called if: // * using SAF on older android version @@ -812,7 +811,7 @@ public class DownloadDialog extends DialogFragment msgBody = R.string.overwrite_unrelated_warning; break; default: - return; + return; // unreachable } final AlertDialog.Builder askDialog = new AlertDialog.Builder(context) diff --git a/app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java b/app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java index 893114dc1..0fcad2958 100644 --- a/app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java +++ b/app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java @@ -290,11 +290,10 @@ public class StoredFileHelper implements Serializable { } // WARNING: DocumentFile.exists() and DocumentFile.isFile() methods are slow - final boolean exists = docFile == null ? ioFile.exists() : docFile.exists(); - // ÂżdocFile.isVirtual() means is no-physical? - final boolean isFile = docFile == null ? ioFile.isFile() : docFile.isFile(); - - return exists && isFile; + // docFile.isVirtual() means it is non-physical? + return docFile == null + ? (ioFile.exists() && ioFile.isFile()) + : (docFile.exists() && docFile.isFile()); } public boolean create() { diff --git a/app/src/main/java/us/shandian/giga/service/DownloadManager.java b/app/src/main/java/us/shandian/giga/service/DownloadManager.java index 283517e01..a2811e72e 100644 --- a/app/src/main/java/us/shandian/giga/service/DownloadManager.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManager.java @@ -106,7 +106,8 @@ public class DownloadManager { } /** - * Loads finished missions from the data source + * Loads finished missions from the data source and forgets finished missions whose file does + * not exist anymore. */ private ArrayList loadFinishedMissions() { ArrayList finishedMissions = mFinishedMissionStore.loadFinishedMissions(); @@ -331,14 +332,29 @@ public class DownloadManager { } /** - * Get a finished mission by its path + * Get the index into {@link #mMissionsFinished} of a finished mission by its path, return + * {@code -1} if there is no such mission. This function also checks if the matched mission's + * file exists, and, if it does not, the related mission is forgotten about (like in {@link + * #loadFinishedMissions()}) and {@code -1} is returned. * - * @param storage where the file possible is stored + * @param storage where the file would be stored * @return the mission index or -1 if no such mission exists */ private int getFinishedMissionIndex(StoredFileHelper storage) { for (int i = 0; i < mMissionsFinished.size(); i++) { if (mMissionsFinished.get(i).storage.equals(storage)) { + // If the file does not exist the mission is not valid anymore. Also checking if + // length == 0 since the file picker may create an empty file before yielding it, + // but that does not mean the file really belonged to a previous mission. + if (!storage.existsAsFile() || storage.length() == 0) { + if (DEBUG) { + Log.d(TAG, "matched downloaded file removed: " + storage.getName()); + } + + mFinishedMissionStore.deleteMission(mMissionsFinished.get(i)); + mMissionsFinished.remove(i); + return -1; // finished mission whose associated file was removed + } return i; } }