From c4116b07e7aa6d60532cdcf334b698437b6725b5 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 12 Jul 2014 15:15:51 -0700 Subject: #361 Make sure to delete from store when deleting song --- src/github/daneren2005/dsub/fragments/SubsonicFragment.java | 7 +++++-- src/github/daneren2005/dsub/service/MediaStoreService.java | 11 +++++++++++ src/github/daneren2005/dsub/util/CacheCleaner.java | 5 +++++ src/github/daneren2005/dsub/util/FileUtil.java | 4 +++- src/github/daneren2005/dsub/util/Util.java | 7 +++++-- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java index 54709ec0..1a5f3ea4 100644 --- a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -55,6 +55,7 @@ import github.daneren2005.dsub.domain.PodcastEpisode; import github.daneren2005.dsub.domain.Share; import github.daneren2005.dsub.service.DownloadFile; import github.daneren2005.dsub.service.DownloadService; +import github.daneren2005.dsub.service.MediaStoreService; import github.daneren2005.dsub.service.MusicService; import github.daneren2005.dsub.service.MusicServiceFactory; import github.daneren2005.dsub.service.OfflineException; @@ -1173,7 +1174,8 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR File dir = FileUtil.getArtistDirectory(context, artist); if(dir == null) return; - Util.recursiveDelete(dir); + MediaStoreService mediaStore = new MediaStoreService(context); + Util.recursiveDelete(dir, mediaStore); if(Util.isOffline(context)) { refresh(); } @@ -1183,7 +1185,8 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR File dir = FileUtil.getAlbumDirectory(context, album); if(dir == null) return; - Util.recursiveDelete(dir); + MediaStoreService mediaStore = new MediaStoreService(context); + Util.recursiveDelete(dir, mediaStore); if(Util.isOffline(context)) { refresh(); } diff --git a/src/github/daneren2005/dsub/service/MediaStoreService.java b/src/github/daneren2005/dsub/service/MediaStoreService.java index d9e84515..ea500e2a 100644 --- a/src/github/daneren2005/dsub/service/MediaStoreService.java +++ b/src/github/daneren2005/dsub/service/MediaStoreService.java @@ -94,6 +94,17 @@ public class MediaStoreService { } } + public void deleteFromMediaStore(File file) { + ContentResolver contentResolver = context.getContentResolver(); + + int n = contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, + MediaStore.MediaColumns.DATA + "=?", + new String[]{file.getAbsolutePath()}); + if (n > 0) { + Log.i(TAG, "Deleting media store row for " + file); + } + } + private void insertAlbumArt(int albumId, DownloadFile downloadFile) { ContentResolver contentResolver = context.getContentResolver(); diff --git a/src/github/daneren2005/dsub/util/CacheCleaner.java b/src/github/daneren2005/dsub/util/CacheCleaner.java index dc38140f..cbc2389a 100644 --- a/src/github/daneren2005/dsub/util/CacheCleaner.java +++ b/src/github/daneren2005/dsub/util/CacheCleaner.java @@ -14,6 +14,8 @@ import android.os.StatFs; import github.daneren2005.dsub.domain.Playlist; import github.daneren2005.dsub.service.DownloadFile; import github.daneren2005.dsub.service.DownloadService; +import github.daneren2005.dsub.service.MediaStoreService; + import java.util.*; /** @@ -27,10 +29,12 @@ public class CacheCleaner { private final Context context; private final DownloadService downloadService; + private final MediaStoreService mediaStore; public CacheCleaner(Context context, DownloadService downloadService) { this.context = context; this.downloadService = downloadService; + this.mediaStore = new MediaStoreService(context); } public void clean() { @@ -112,6 +116,7 @@ public class CacheCleaner { long size = file.length(); if (Util.delete(file)) { bytesDeleted += size; + mediaStore.deleteFromMediaStore(file); } } } diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index a73d9718..fa259b96 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -55,6 +55,7 @@ import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.MusicFolder; import github.daneren2005.dsub.domain.PodcastChannel; import github.daneren2005.dsub.domain.PodcastEpisode; +import github.daneren2005.dsub.service.MediaStoreService; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; @@ -365,7 +366,8 @@ public class FileUtil { } public static boolean deleteMusicDirectory(Context context) { File musicDirectory = FileUtil.getMusicDirectory(context); - return Util.recursiveDelete(musicDirectory); + MediaStoreService mediaStore = new MediaStoreService(context); + return Util.recursiveDelete(musicDirectory, mediaStore); } public static void deleteSerializedCache(Context context) { for(File file: context.getCacheDir().listFiles()) { diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java index cea5813e..f8e4cf95 100644 --- a/src/github/daneren2005/dsub/util/Util.java +++ b/src/github/daneren2005/dsub/util/Util.java @@ -68,6 +68,7 @@ import github.daneren2005.dsub.provider.DSubWidgetProvider; import github.daneren2005.dsub.receiver.MediaButtonIntentReceiver; import github.daneren2005.dsub.service.DownloadFile; import github.daneren2005.dsub.service.DownloadService; +import github.daneren2005.dsub.service.MediaStoreService; import org.apache.http.HttpEntity; @@ -615,18 +616,20 @@ public final class Util { } return true; } - public static boolean recursiveDelete(File dir) { + public static boolean recursiveDelete(File dir, MediaStoreService mediaStore) { if (dir != null && dir.exists()) { File[] list = dir.listFiles(); if(list != null) { for(File file: list) { if(file.isDirectory()) { - if(!recursiveDelete(file)) { + if(!recursiveDelete(file, mediaStore)) { return false; } } else if(file.exists()) { if(!file.delete()) { return false; + } else { + mediaStore.deleteFromMediaStore(file); } } } -- cgit v1.2.3