diff options
-rw-r--r-- | src/github/daneren2005/dsub/service/DownloadFile.java | 41 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/service/MediaStoreService.java | 13 |
2 files changed, 44 insertions, 10 deletions
diff --git a/src/github/daneren2005/dsub/service/DownloadFile.java b/src/github/daneren2005/dsub/service/DownloadFile.java index 3fe3ba93..6ac6e90e 100644 --- a/src/github/daneren2005/dsub/service/DownloadFile.java +++ b/src/github/daneren2005/dsub/service/DownloadFile.java @@ -32,6 +32,7 @@ import android.os.PowerManager; import android.util.DisplayMetrics; import android.util.Log; import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.Util; @@ -263,15 +264,22 @@ public class DownloadFile implements BufferFile { public void delete() { cancelDownload(); + + // Remove from mediaStore BEFORE deleting file since it calls getCompleteFile + deleteFromStore(); + + // Delete all possible versions of the file Util.delete(partialFile); Util.delete(completeFile); Util.delete(saveFile); - mediaStoreService.deleteFromMediaStore(this); } public void unpin() { if (saveFile.exists()) { + // Delete old store entry before renaming to pinned file + deleteFromStore(); saveFile.renameTo(completeFile); + saveToStore(); } } @@ -305,14 +313,17 @@ public class DownloadFile implements BufferFile { public void setPlaying(boolean isPlaying) { try { if(saveWhenDone && !isPlaying) { + deleteFromStore(); Util.renameFile(completeFile, saveFile); + saveToStore(); saveWhenDone = false; } else if(completeWhenDone && !isPlaying) { if(save) { Util.renameFile(partialFile, saveFile); - mediaStoreService.saveInMediaStore(DownloadFile.this); + saveToStore(); } else { Util.renameFile(partialFile, completeFile); + saveToStore(); } completeWhenDone = false; } @@ -325,6 +336,7 @@ public class DownloadFile implements BufferFile { public void renamePartial() { try { Util.renameFile(partialFile, completeFile); + saveToStore(); } catch(IOException ex) { Log.w(TAG, "Failed to rename file " + partialFile + " to " + completeFile, ex); } @@ -332,6 +344,23 @@ public class DownloadFile implements BufferFile { public boolean getPlaying() { return isPlaying; } + + private void deleteFromStore() { + try { + mediaStoreService.deleteFromMediaStore(this); + } catch(Exception e) { + Log.w(TAG, "Failed to remove from store", e); + } + } + private void saveToStore() { + if(!Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_HIDE_MEDIA, false)) { + try { + mediaStoreService.saveInMediaStore(this); + } catch(Exception e) { + Log.w(TAG, "Failed to save in media store", e); + } + } + } @Override public String toString() { @@ -372,7 +401,9 @@ public class DownloadFile implements BufferFile { if(isPlaying) { saveWhenDone = true; } else { + deleteFromStore(); Util.renameFile(completeFile, saveFile); + DownloadFile.this.saveToStore(); } } else { Log.i(TAG, completeFile + " already exists. Skipping."); @@ -429,14 +460,10 @@ public class DownloadFile implements BufferFile { } else { if(save) { Util.renameFile(partialFile, saveFile); - try { - mediaStoreService.saveInMediaStore(DownloadFile.this); - } catch(Exception e) { - Log.w(TAG, "Failed to save in media store", e); - } } else { Util.renameFile(partialFile, completeFile); } + DownloadFile.this.saveToStore(); } } catch(InterruptedException x) { diff --git a/src/github/daneren2005/dsub/service/MediaStoreService.java b/src/github/daneren2005/dsub/service/MediaStoreService.java index 4de77bf2..d9e84515 100644 --- a/src/github/daneren2005/dsub/service/MediaStoreService.java +++ b/src/github/daneren2005/dsub/service/MediaStoreService.java @@ -56,10 +56,17 @@ public class MediaStoreService { values.put(MediaStore.MediaColumns.TITLE, song.getTitle()); values.put(MediaStore.Audio.AudioColumns.ARTIST, song.getArtist()); values.put(MediaStore.Audio.AudioColumns.ALBUM, song.getAlbum()); - values.put(MediaStore.Audio.AudioColumns.TRACK, song.getTrack()); - values.put(MediaStore.Audio.AudioColumns.YEAR, song.getYear()); values.put(MediaStore.MediaColumns.DATA, songFile.getAbsolutePath()); values.put(MediaStore.MediaColumns.MIME_TYPE, song.getContentType()); + if(song.getDuration() != null) { + values.put(MediaStore.Audio.AudioColumns.DURATION, song.getDuration() * 1000L); + } + if(song.getTrack() != null) { + values.put(MediaStore.Audio.AudioColumns.TRACK, song.getTrack()); + } + if(song.getYear() != null) { + values.put(MediaStore.Audio.AudioColumns.YEAR, song.getYear()); + } values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, 1); Uri uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values); @@ -106,4 +113,4 @@ public class MediaStoreService { cursor.close(); } -}
\ No newline at end of file +} |