Skip to content

Commit

Permalink
#1002 Delete from media store before renaming file since in Android 1…
Browse files Browse the repository at this point in the history
…1 it deletes the actual file instead of just the media store record
  • Loading branch information
daneren2005 committed Sep 12, 2020
1 parent b375100 commit 04f1c5a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,11 @@ public void setPlaying(boolean isPlaying) {
saveWhenDone = false;
} else if(completeWhenDone && !isPlaying) {
if(save) {
deleteFromStore();
Util.renameFile(partialFile, saveFile);
saveToStore();
} else {
deleteFromStore();
Util.renameFile(partialFile, completeFile);
saveToStore();
}
Expand All @@ -337,6 +339,7 @@ public void setPlaying(boolean isPlaying) {
}
public void renamePartial() {
try {
deleteFromStore();
Util.renameFile(partialFile, completeFile);
saveToStore();
} catch(IOException ex) {
Expand All @@ -348,10 +351,12 @@ public boolean getPlaying() {
}

private void deleteFromStore() {
try {
mediaStoreService.deleteFromMediaStore(this);
} catch(Exception e) {
Log.w(TAG, "Failed to remove from store", e);
if(!Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_HIDE_MEDIA, false)) {
try {
mediaStoreService.deleteFromMediaStore(this);
} catch (Exception e) {
Log.w(TAG, "Failed to remove from store", e);
}
}
}
private void saveToStore() {
Expand Down Expand Up @@ -490,6 +495,7 @@ public Void doInBackground() throws InterruptedException {
if(isPlaying) {
completeWhenDone = true;
} else {
deleteFromStore();
if(save) {
Util.renameFile(partialFile, saveFile);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public void saveInMediaStore(DownloadFile downloadFile) {
MusicDirectory.Entry song = downloadFile.getSong();
File songFile = downloadFile.getCompleteFile();

// Delete existing row in case the song has been downloaded before.
deleteFromMediaStore(downloadFile);

ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
if(!song.isVideo()) {
Expand All @@ -76,6 +73,10 @@ public void saveInMediaStore(DownloadFile downloadFile) {
values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, 1);

Uri uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
if(uri == null) {
Log.e(TAG, "URI for media store is null");
return;
}

// Look up album, and add cover art if found.
Cursor cursor = contentResolver.query(uri, new String[]{MediaStore.Audio.AudioColumns.ALBUM_ID}, null, null, null);
Expand Down

0 comments on commit 04f1c5a

Please sign in to comment.