From 158f535e80755ec5c2f58e506548bd44e74d1ab0 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 12 Jul 2014 15:40:37 -0700 Subject: #361 Just rename in media store for pinning/unpinning --- src/github/daneren2005/dsub/service/DownloadFile.java | 16 ++++++++++------ .../daneren2005/dsub/service/MediaStoreService.java | 15 +++++++++++++++ .../dsub/service/sync/PlaylistSyncAdapter.java | 2 +- .../dsub/service/sync/StarredSyncAdapter.java | 2 +- src/github/daneren2005/dsub/util/FileUtil.java | 8 +++++++- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/github/daneren2005/dsub/service/DownloadFile.java b/src/github/daneren2005/dsub/service/DownloadFile.java index 6ac6e90e..6ded4343 100644 --- a/src/github/daneren2005/dsub/service/DownloadFile.java +++ b/src/github/daneren2005/dsub/service/DownloadFile.java @@ -277,9 +277,8 @@ public class DownloadFile implements BufferFile { public void unpin() { if (saveFile.exists()) { // Delete old store entry before renaming to pinned file - deleteFromStore(); saveFile.renameTo(completeFile); - saveToStore(); + renameInStore(saveFile, completeFile); } } @@ -313,9 +312,8 @@ public class DownloadFile implements BufferFile { public void setPlaying(boolean isPlaying) { try { if(saveWhenDone && !isPlaying) { - deleteFromStore(); Util.renameFile(completeFile, saveFile); - saveToStore(); + renameInStore(completeFile, saveFile); saveWhenDone = false; } else if(completeWhenDone && !isPlaying) { if(save) { @@ -361,6 +359,13 @@ public class DownloadFile implements BufferFile { } } } + private void renameInStore(File start, File end) { + try { + mediaStoreService.renameInMediaStore(start, end); + } catch(Exception e) { + Log.w(TAG, "Failed to rename in store", e); + } + } @Override public String toString() { @@ -401,9 +406,8 @@ public class DownloadFile implements BufferFile { if(isPlaying) { saveWhenDone = true; } else { - deleteFromStore(); Util.renameFile(completeFile, saveFile); - DownloadFile.this.saveToStore(); + renameInStore(completeFile, saveFile); } } else { Log.i(TAG, completeFile + " already exists. Skipping."); diff --git a/src/github/daneren2005/dsub/service/MediaStoreService.java b/src/github/daneren2005/dsub/service/MediaStoreService.java index ea500e2a..c1935ddc 100644 --- a/src/github/daneren2005/dsub/service/MediaStoreService.java +++ b/src/github/daneren2005/dsub/service/MediaStoreService.java @@ -105,6 +105,21 @@ public class MediaStoreService { } } + public void renameInMediaStore(File start, File end) { + ContentResolver contentResolver = context.getContentResolver(); + + ContentValues values = new ContentValues(); + values.put(MediaStore.MediaColumns.DATA, end.getAbsolutePath()); + + int n = contentResolver.update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, + values, + MediaStore.MediaColumns.DATA + "=?", + new String[]{start.getAbsolutePath()}); + if (n > 0) { + Log.i(TAG, "Rename media store row for " + start + " to " + end); + } + } + private void insertAlbumArt(int albumId, DownloadFile downloadFile) { ContentResolver contentResolver = context.getContentResolver(); diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java index 68bfdcb6..bd6d056b 100644 --- a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java @@ -102,7 +102,7 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter { if(origPathList.size() > 0) { for(String path: origPathList) { File saveFile = new File(path); - FileUtil.unpinSong(saveFile); + FileUtil.unpinSong(context, saveFile); cachedPlaylist.synced.remove(path); } diff --git a/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java index b99c501d..7cff68aa 100644 --- a/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java @@ -66,7 +66,7 @@ public class StarredSyncAdapter extends SubsonicSyncAdapter { for(String path: oldSyncedList) { File saveFile = new File(path); - FileUtil.unpinSong(saveFile); + FileUtil.unpinSong(context, saveFile); } SyncUtil.setSyncedStarred(syncedList, context, instance); diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index fa259b96..ead17f5d 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -377,13 +377,19 @@ public class FileUtil { } } - public static void unpinSong(File saveFile) { + public static void unpinSong(Context context, File saveFile) { // Unpin file, rename to .complete File completeFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) + ".complete." + FileUtil.getExtension(saveFile.getName())); if(!saveFile.renameTo(completeFile)) { Log.w(TAG, "Failed to rename " + saveFile + " to " + completeFile); + } else { + try { + new MediaStoreService(context).renameInMediaStore(completeFile, saveFile); + } catch(Exception e) { + Log.w(TAG, "Failed to write to media store"); + } } } -- cgit v1.2.3