diff --git a/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java b/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java index 773ff92d1..e93e83a87 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java +++ b/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java @@ -80,7 +80,7 @@ public abstract class Postprocessing implements Serializable { private transient DownloadMission mission; - private File tempFile; + private transient File tempFile; Postprocessing(boolean reserveSpace, boolean worksOnSameFile, String algorithmName) { this.reserveSpace = reserveSpace; @@ -95,8 +95,12 @@ public abstract class Postprocessing implements Serializable { public void cleanupTemporalDir() { if (tempFile != null && tempFile.exists()) { - //noinspection ResultOfMethodCallIgnored - tempFile.delete(); + try { + //noinspection ResultOfMethodCallIgnored + tempFile.delete(); + } catch (Exception e) { + // nothing to do + } } } 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 e8bc468e9..994c6ee63 100644 --- a/app/src/main/java/us/shandian/giga/service/DownloadManager.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManager.java @@ -139,6 +139,9 @@ public class DownloadManager { Log.d(TAG, "Loading pending downloads from directory: " + mPendingMissionsDir.getAbsolutePath()); } + File tempDir = pickAvailableTemporalDir(ctx); + Log.i(TAG, "using '" + tempDir + "' as temporal directory"); + for (File sub : subs) { if (!sub.isFile()) continue; if (sub.getName().equals(".tmp")) continue; @@ -184,7 +187,7 @@ public class DownloadManager { if (mis.psAlgorithm != null) { mis.psAlgorithm.cleanupTemporalDir(); - mis.psAlgorithm.setTemporalDir(pickAvailableTemporalDir(ctx)); + mis.psAlgorithm.setTemporalDir(tempDir); } mis.metadata = sub; @@ -513,13 +516,21 @@ public class DownloadManager { } static File pickAvailableTemporalDir(@NonNull Context ctx) { - if (isDirectoryAvailable(ctx.getExternalFilesDir(null))) - return ctx.getExternalFilesDir(null); - else if (isDirectoryAvailable(ctx.getFilesDir())) - return ctx.getFilesDir(); + File dir = ctx.getExternalFilesDir(null); + if (isDirectoryAvailable(dir)) return dir; + + dir = ctx.getFilesDir(); + if (isDirectoryAvailable(dir)) return dir; // this never should happen - return ctx.getDir("tmp", Context.MODE_PRIVATE); + dir = ctx.getDir("muxing_tmp", Context.MODE_PRIVATE); + if (isDirectoryAvailable(dir)) return dir; + + // fallback to cache dir + dir = ctx.getCacheDir(); + if (isDirectoryAvailable(dir)) return dir; + + throw new RuntimeException("Not temporal directories are available"); } @Nullable