Skip to content

Commit 23793e3

Browse files
fix: unfinished downloads disappear from the downloads list after app gets killed
1 parent 9bc8139 commit 23793e3

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

app/src/main/java/us/shandian/giga/get/DownloadMission.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ public void psContinue(boolean recover) {
661661
* @return {@code true}, if storage is invalid and cannot be used
662662
*/
663663
public boolean hasInvalidStorage() {
664-
return errCode == ERROR_PROGRESS_LOST || storage == null || !storage.existsAsFile();
664+
// Don't consider ERROR_PROGRESS_LOST as invalid storage - it can be recovered
665+
return storage == null || !storage.existsAsFile();
665666
}
666667

667668
/**

app/src/main/java/us/shandian/giga/service/DownloadManager.java

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import us.shandian.giga.util.Utility;
2525

2626
import static org.schabi.newpipe.BuildConfig.DEBUG;
27+
import static us.shandian.giga.get.DownloadMission.ERROR_NOTHING;
28+
import static us.shandian.giga.get.DownloadMission.ERROR_PROGRESS_LOST;
2729

2830
public class DownloadManager {
2931
private static final String TAG = DownloadManager.class.getSimpleName();
@@ -149,12 +151,31 @@ private void loadPendingMissions(Context ctx) {
149151
if (sub.getName().equals(".tmp")) continue;
150152

151153
DownloadMission mis = Utility.readFromFile(sub);
152-
if (mis == null || mis.isFinished() || mis.hasInvalidStorage()) {
154+
if (mis == null) {
153155
//noinspection ResultOfMethodCallIgnored
154156
sub.delete();
155157
continue;
156158
}
157159

160+
// DON'T delete missions that are truly finished - let them be moved to finished list
161+
if (mis.isFinished()) {
162+
// Move to finished missions instead of deleting
163+
setFinished(mis);
164+
//noinspection ResultOfMethodCallIgnored
165+
sub.delete();
166+
continue;
167+
}
168+
169+
// DON'T delete missions with storage issues - try to recover them
170+
if (mis.hasInvalidStorage() && mis.errCode != ERROR_PROGRESS_LOST) {
171+
// Only delete if it's truly unrecoverable (not just progress lost)
172+
if (mis.storage == null && mis.errCode != ERROR_PROGRESS_LOST) {
173+
//noinspection ResultOfMethodCallIgnored
174+
sub.delete();
175+
continue;
176+
}
177+
}
178+
158179
mis.threads = new Thread[0];
159180

160181
boolean exists;
@@ -163,16 +184,13 @@ private void loadPendingMissions(Context ctx) {
163184
exists = !mis.storage.isInvalid() && mis.storage.existsAsFile();
164185
} catch (Exception ex) {
165186
Log.e(TAG, "Failed to load the file source of " + mis.storage.toString(), ex);
166-
mis.storage.invalidate();
187+
// Don't invalidate storage immediately - try to recover first
167188
exists = false;
168189
}
169190

170191
if (mis.isPsRunning()) {
171192
if (mis.psAlgorithm.worksOnSameFile) {
172193
// Incomplete post-processing results in a corrupted download file
173-
// because the selected algorithm works on the same file to save space.
174-
// the file will be deleted if the storage API
175-
// is Java IO (avoid showing the "Save as..." dialog)
176194
if (exists && mis.storage.isDirect() && !mis.storage.delete())
177195
Log.w(TAG, "Unable to delete incomplete download file: " + sub.getPath());
178196
}
@@ -181,10 +199,11 @@ private void loadPendingMissions(Context ctx) {
181199
mis.errCode = DownloadMission.ERROR_POSTPROCESSING_STOPPED;
182200
} else if (!exists) {
183201
tryRecover(mis);
184-
185-
// the progress is lost, reset mission state
186-
if (mis.isInitialized())
187-
mis.resetState(true, true, DownloadMission.ERROR_PROGRESS_LOST);
202+
// Keep the mission even if recovery fails - don't reset to ERROR_PROGRESS_LOST
203+
// This allows user to see the failed download and potentially retry
204+
if (mis.isInitialized() && mis.errCode == ERROR_NOTHING) {
205+
mis.resetState(true, true, ERROR_PROGRESS_LOST);
206+
}
188207
}
189208

190209
if (mis.psAlgorithm != null) {
@@ -446,7 +465,7 @@ boolean runMissions() {
446465
continue;
447466

448467
resumeMission(mission);
449-
if (mission.errCode != DownloadMission.ERROR_NOTHING) continue;
468+
if (mission.errCode != ERROR_NOTHING) continue;
450469

451470
if (mPrefQueueLimit) return true;
452471
flag = true;
@@ -510,6 +529,15 @@ void updateMaximumAttempts() {
510529
}
511530
}
512531

532+
public boolean canRecoverMission(DownloadMission mission) {
533+
if (mission == null) return false;
534+
535+
// Can recover missions with progress lost or storage issues
536+
return mission.errCode == ERROR_PROGRESS_LOST ||
537+
mission.storage == null ||
538+
!mission.storage.existsAsFile();
539+
}
540+
513541
public MissionState checkForExistingMission(StoredFileHelper storage) {
514542
synchronized (this) {
515543
DownloadMission pending = getPendingMission(storage);
@@ -582,8 +610,16 @@ private ArrayList<Object> getSpecialItems() {
582610
ArrayList<Mission> finished = new ArrayList<>(mMissionsFinished);
583611
List<Mission> remove = new ArrayList<>(hidden);
584612

585-
// hide missions (if required)
586-
remove.removeIf(mission -> pending.remove(mission) || finished.remove(mission));
613+
// Don't hide recoverable missions
614+
remove.removeIf(mission -> {
615+
if (mission instanceof DownloadMission) {
616+
DownloadMission dm = (DownloadMission) mission;
617+
if (canRecoverMission(dm)) {
618+
return false; // Don't remove recoverable missions
619+
}
620+
}
621+
return pending.remove(mission) || finished.remove(mission);
622+
});
587623

588624
int fakeTotal = pending.size();
589625
if (fakeTotal > 0) fakeTotal++;

0 commit comments

Comments
 (0)