aboutsummaryrefslogtreecommitdiff
path: root/subsonic-android
diff options
context:
space:
mode:
authorowner <owner@DeeDee-Laptop>2012-12-02 17:13:56 -0800
committerowner <owner@DeeDee-Laptop>2012-12-02 17:13:56 -0800
commitdc3ab40ef2f9e227a932f873e675514ce3c420f2 (patch)
tree2a418470c213a616ff9e10ca0724f8f6ddb0885b /subsonic-android
parent9f9bff0d03e0d1b1c4a00321bb377b3c31809e64 (diff)
downloaddsub-dc3ab40ef2f9e227a932f873e675514ce3c420f2.tar.gz
dsub-dc3ab40ef2f9e227a932f873e675514ce3c420f2.tar.bz2
dsub-dc3ab40ef2f9e227a932f873e675514ce3c420f2.zip
Don't copy then delete partials, rename + wait if being played
Diffstat (limited to 'subsonic-android')
-rw-r--r--subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java49
-rw-r--r--subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java11
-rw-r--r--subsonic-android/src/github/daneren2005/dsub/util/Util.java7
3 files changed, 55 insertions, 12 deletions
diff --git a/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java b/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java
index b2a6de57..484ec548 100644
--- a/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java
+++ b/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java
@@ -56,6 +56,9 @@ public class DownloadFile {
private boolean save;
private boolean failed;
private int bitRate;
+ private boolean isPlaying = false;
+ private boolean saveWhenDone = false;
+ private boolean completeWhenDone = false;
public DownloadFile(Context context, MusicDirectory.Entry song, boolean save) {
this.context = context;
@@ -181,6 +184,30 @@ public class DownloadFile {
}
}
}
+
+ public void setPlaying(boolean isPlaying) {
+ try {
+ if(saveWhenDone && isPlaying == false) {
+ Util.renameFile(completeFile, saveFile);
+ saveWhenDone = false;
+ } else if(completeWhenDone && isPlaying == false) {
+ if(save) {
+ Util.renameFile(partialFile, saveFile);
+ mediaStoreService.saveInMediaStore(DownloadFile.this);
+ } else {
+ Util.renameFile(partialFile, completeFile);
+ }
+ completeWhenDone = false;
+ }
+ } catch(IOException ex) {
+ Log.w(TAG, "Failed to rename file " + completeFile + " to " + saveFile);
+ }
+
+ this.isPlaying = isPlaying;
+ }
+ public boolean getPlaying() {
+ return isPlaying;
+ }
@Override
public String toString() {
@@ -214,7 +241,11 @@ public class DownloadFile {
}
if (completeFile.exists()) {
if (save) {
- Util.atomicCopy(completeFile, saveFile);
+ if(isPlaying) {
+ saveWhenDone = true;
+ } else {
+ Util.renameFile(completeFile, saveFile);
+ }
} else {
Log.i(TAG, completeFile + " already exists. Skipping.");
}
@@ -243,12 +274,16 @@ public class DownloadFile {
downloadAndSaveCoverArt(musicService);
- if (save) {
- Util.atomicCopy(partialFile, saveFile);
- mediaStoreService.saveInMediaStore(DownloadFile.this);
- } else {
- Util.atomicCopy(partialFile, completeFile);
- }
+ if(isPlaying) {
+ completeWhenDone = true;
+ } else {
+ if(save) {
+ Util.renameFile(partialFile, saveFile);
+ mediaStoreService.saveInMediaStore(DownloadFile.this);
+ } else {
+ Util.renameFile(partialFile, completeFile);
+ }
+ }
} catch (Exception x) {
Util.close(out);
diff --git a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java
index a718ad9d..ba6bff04 100644
--- a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java
+++ b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java
@@ -258,6 +258,7 @@ public class DownloadServiceImpl extends Service implements DownloadService {
} else {
if (currentPlaying == null) {
currentPlaying = downloadList.get(0);
+ currentPlaying.setPlaying(true);
}
checkDownloads();
}
@@ -467,11 +468,16 @@ public class DownloadServiceImpl extends Service implements DownloadService {
}
synchronized void setCurrentPlaying(DownloadFile currentPlaying, boolean showNotification) {
+ if(this.currentPlaying != null) {
+ this.currentPlaying.setPlaying(false);
+ }
this.currentPlaying = currentPlaying;
if (currentPlaying != null) {
Util.requestAudioFocus(this);
Util.broadcastNewTrackInfo(this, currentPlaying.getSong());
+ currentPlaying.setPlaying(true);
+ mRemoteControl.updateMetadata(this, currentPlaying.getSong());
} else {
Util.broadcastNewTrackInfo(this, null);
}
@@ -481,11 +487,6 @@ public class DownloadServiceImpl extends Service implements DownloadService {
} else {
Util.hidePlayingNotification(this, this, handler);
}
-
- if(currentPlaying != null) {
- mRemoteControl.updateMetadata(this, currentPlaying.getSong());
- }
-
}
@Override
diff --git a/subsonic-android/src/github/daneren2005/dsub/util/Util.java b/subsonic-android/src/github/daneren2005/dsub/util/Util.java
index 2bdd969e..13341d64 100644
--- a/subsonic-android/src/github/daneren2005/dsub/util/Util.java
+++ b/subsonic-android/src/github/daneren2005/dsub/util/Util.java
@@ -329,6 +329,13 @@ public final class Util {
delete(tmp);
}
}
+ public static void renameFile(File from, File to) throws IOException {
+ if(from.renameTo(to)) {
+ Log.i(TAG, "Renamed " + from + " to " + to);
+ } else {
+ atomicCopy(from, to);
+ }
+ }
public static void close(Closeable closeable) {
try {