From 9282cce6a84d157615e2218f991ffed4efd7e522 Mon Sep 17 00:00:00 2001 From: whistlingwoods <72640314+whistlingwoods@users.noreply.github.com> Date: Sun, 17 Aug 2025 20:41:03 +0530 Subject: [PATCH 1/2] fix: unfinished downloads disappear from the downloads list after app gets killed Author: InfinityLoop1308 Adapted for NewPipe from a fork's this commit https://github.com/InfinityLoop1308/PipePipeClient/commit/1cf059ce5e947aadb6dacd93b2ac9ca81b79cf46 --- .../us/shandian/giga/get/DownloadMission.java | 3 +- .../giga/service/DownloadManager.java | 60 +++++++++++++++---- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/us/shandian/giga/get/DownloadMission.java b/app/src/main/java/us/shandian/giga/get/DownloadMission.java index 04930b002..54340ce5d 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadMission.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadMission.java @@ -661,7 +661,8 @@ public class DownloadMission extends Mission { * @return {@code true}, if storage is invalid and cannot be used */ public boolean hasInvalidStorage() { - return errCode == ERROR_PROGRESS_LOST || storage == null || !storage.existsAsFile(); + // Don't consider ERROR_PROGRESS_LOST as invalid storage - it can be recovered + return storage == null || !storage.existsAsFile(); } /** 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 9b90fa14b..6aff4daf2 100644 --- a/app/src/main/java/us/shandian/giga/service/DownloadManager.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManager.java @@ -24,6 +24,8 @@ import org.schabi.newpipe.streams.io.StoredFileHelper; import us.shandian.giga.util.Utility; import static org.schabi.newpipe.BuildConfig.DEBUG; +import static us.shandian.giga.get.DownloadMission.ERROR_NOTHING; +import static us.shandian.giga.get.DownloadMission.ERROR_PROGRESS_LOST; public class DownloadManager { private static final String TAG = DownloadManager.class.getSimpleName(); @@ -149,12 +151,31 @@ public class DownloadManager { if (sub.getName().equals(".tmp")) continue; DownloadMission mis = Utility.readFromFile(sub); - if (mis == null || mis.isFinished() || mis.hasInvalidStorage()) { + if (mis == null) { //noinspection ResultOfMethodCallIgnored sub.delete(); continue; } + // DON'T delete missions that are truly finished - let them be moved to finished list + if (mis.isFinished()) { + // Move to finished missions instead of deleting + setFinished(mis); + //noinspection ResultOfMethodCallIgnored + sub.delete(); + continue; + } + + // DON'T delete missions with storage issues - try to recover them + if (mis.hasInvalidStorage() && mis.errCode != ERROR_PROGRESS_LOST) { + // Only delete if it's truly unrecoverable (not just progress lost) + if (mis.storage == null && mis.errCode != ERROR_PROGRESS_LOST) { + //noinspection ResultOfMethodCallIgnored + sub.delete(); + continue; + } + } + mis.threads = new Thread[0]; boolean exists; @@ -163,16 +184,13 @@ public class DownloadManager { exists = !mis.storage.isInvalid() && mis.storage.existsAsFile(); } catch (Exception ex) { Log.e(TAG, "Failed to load the file source of " + mis.storage.toString(), ex); - mis.storage.invalidate(); + // Don't invalidate storage immediately - try to recover first exists = false; } if (mis.isPsRunning()) { if (mis.psAlgorithm.worksOnSameFile) { // Incomplete post-processing results in a corrupted download file - // because the selected algorithm works on the same file to save space. - // the file will be deleted if the storage API - // is Java IO (avoid showing the "Save as..." dialog) if (exists && mis.storage.isDirect() && !mis.storage.delete()) Log.w(TAG, "Unable to delete incomplete download file: " + sub.getPath()); } @@ -181,10 +199,11 @@ public class DownloadManager { mis.errCode = DownloadMission.ERROR_POSTPROCESSING_STOPPED; } else if (!exists) { tryRecover(mis); - - // the progress is lost, reset mission state - if (mis.isInitialized()) - mis.resetState(true, true, DownloadMission.ERROR_PROGRESS_LOST); + // Keep the mission even if recovery fails - don't reset to ERROR_PROGRESS_LOST + // This allows user to see the failed download and potentially retry + if (mis.isInitialized() && mis.errCode == ERROR_NOTHING) { + mis.resetState(true, true, ERROR_PROGRESS_LOST); + } } if (mis.psAlgorithm != null) { @@ -446,7 +465,7 @@ public class DownloadManager { continue; resumeMission(mission); - if (mission.errCode != DownloadMission.ERROR_NOTHING) continue; + if (mission.errCode != ERROR_NOTHING) continue; if (mPrefQueueLimit) return true; flag = true; @@ -510,6 +529,15 @@ public class DownloadManager { } } + public boolean canRecoverMission(DownloadMission mission) { + if (mission == null) return false; + + // Can recover missions with progress lost or storage issues + return mission.errCode == ERROR_PROGRESS_LOST || + mission.storage == null || + !mission.storage.existsAsFile(); + } + public MissionState checkForExistingMission(StoredFileHelper storage) { synchronized (this) { DownloadMission pending = getPendingMission(storage); @@ -582,8 +610,16 @@ public class DownloadManager { ArrayList finished = new ArrayList<>(mMissionsFinished); List remove = new ArrayList<>(hidden); - // hide missions (if required) - remove.removeIf(mission -> pending.remove(mission) || finished.remove(mission)); + // Don't hide recoverable missions + remove.removeIf(mission -> { + if (mission instanceof DownloadMission) { + DownloadMission dm = (DownloadMission) mission; + if (canRecoverMission(dm)) { + return false; // Don't remove recoverable missions + } + } + return pending.remove(mission) || finished.remove(mission); + }); int fakeTotal = pending.size(); if (fakeTotal > 0) fakeTotal++; From 21e24c9e34c7b0fb5aaee0e0eb7c36d985f76808 Mon Sep 17 00:00:00 2001 From: whistlingwoods <72640314+whistlingwoods@users.noreply.github.com> Date: Sat, 6 Sep 2025 19:14:15 +0530 Subject: [PATCH 2/2] Apply review suggestions --- .../java/us/shandian/giga/service/DownloadManager.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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 6aff4daf2..8d841a207 100644 --- a/app/src/main/java/us/shandian/giga/service/DownloadManager.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManager.java @@ -169,7 +169,7 @@ public class DownloadManager { // DON'T delete missions with storage issues - try to recover them if (mis.hasInvalidStorage() && mis.errCode != ERROR_PROGRESS_LOST) { // Only delete if it's truly unrecoverable (not just progress lost) - if (mis.storage == null && mis.errCode != ERROR_PROGRESS_LOST) { + if (mis.storage == null) { //noinspection ResultOfMethodCallIgnored sub.delete(); continue; @@ -612,11 +612,8 @@ public class DownloadManager { // Don't hide recoverable missions remove.removeIf(mission -> { - if (mission instanceof DownloadMission) { - DownloadMission dm = (DownloadMission) mission; - if (canRecoverMission(dm)) { - return false; // Don't remove recoverable missions - } + if (mission instanceof DownloadMission dm && canRecoverMission(dm)) { + return false; // Don't remove recoverable missions } return pending.remove(mission) || finished.remove(mission); });