From 46c3d4e8c1a4031837569916318328e4fc8ede76 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 12 Nov 2023 23:00:06 +0100 Subject: [PATCH] Fix file deletion (#6758) - When deleting local folders, don't delete files - Don't try to delete files twice - Fix deleting non-local feeds --- .../antennapod/core/storage/DBWriter.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index 1453f701d..c5c656705 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -116,7 +116,16 @@ public class DBWriter { Log.i(TAG, String.format(Locale.US, "Requested to delete FeedMedia [id=%d, title=%s, downloaded=%s", media.getId(), media.getEpisodeTitle(), media.isDownloaded())); boolean localDelete = false; - if (media.isDownloaded() || media.getFile_url() != null) { + if (media.getFile_url() != null && media.getFile_url().startsWith("content://")) { + // Local feed + DocumentFile documentFile = DocumentFile.fromSingleUri(context, Uri.parse(media.getFile_url())); + if (documentFile == null || !documentFile.exists() || !documentFile.delete()) { + EventBus.getDefault().post(new MessageEvent(context.getString(R.string.delete_local_failed))); + return false; + } + media.setFile_url(null); + localDelete = true; + } else if (media.getFile_url() != null) { // delete downloaded media file File mediaFile = new File(media.getFile_url()); if (mediaFile.exists() && !mediaFile.delete()) { @@ -131,15 +140,6 @@ public class DBWriter { adapter.open(); adapter.setMedia(media); adapter.close(); - } else if (media.getFile_url().startsWith("content://")) { - // Local feed - DocumentFile documentFile = DocumentFile.fromSingleUri( - context, Uri.parse(media.getFile_url())); - if (documentFile == null || !documentFile.exists() || !documentFile.delete()) { - EventBus.getDefault().post(new MessageEvent(context.getString(R.string.delete_local_failed))); - return false; - } - localDelete = true; } if (media.getId() == PlaybackPreferences.getCurrentlyPlayingFeedMediaId()) { @@ -224,10 +224,12 @@ public class DBWriter { PlaybackPreferences.writeNoMediaPlaying(); IntentUtils.sendLocalBroadcast(context, PlaybackServiceInterface.ACTION_SHUTDOWN_PLAYBACK_SERVICE); } - if (item.getMedia().isDownloaded()) { - deleteFeedMediaSynchronous(context, item.getMedia()); + if (!item.getFeed().isLocalFeed()) { + DownloadServiceInterface.get().cancel(context, item.getMedia()); + if (item.getMedia().isDownloaded()) { + deleteFeedMediaSynchronous(context, item.getMedia()); + } } - DownloadServiceInterface.get().cancel(context, item.getMedia()); } }