aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/service/DownloadFile.java19
-rw-r--r--src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java25
2 files changed, 31 insertions, 13 deletions
diff --git a/src/github/daneren2005/dsub/service/DownloadFile.java b/src/github/daneren2005/dsub/service/DownloadFile.java
index b8a436cb..dc1c7cbd 100644
--- a/src/github/daneren2005/dsub/service/DownloadFile.java
+++ b/src/github/daneren2005/dsub/service/DownloadFile.java
@@ -66,6 +66,7 @@ public class DownloadFile implements BufferFile {
private boolean completeWhenDone = false;
private Long contentLength = null;
private long currentSpeed = 0;
+ private boolean rateLimit = false;
public DownloadFile(Context context, MusicDirectory.Entry song, boolean save) {
this.context = context;
@@ -149,10 +150,12 @@ public class DownloadFile implements BufferFile {
}
public synchronized void download() {
+ rateLimit = false;
preDownload();
downloadTask.execute();
}
public synchronized void downloadNow(MusicService musicService) {
+ rateLimit = true;
preDownload();
downloadTask.setMusicService(musicService);
try {
@@ -522,6 +525,7 @@ public class DownloadFile implements BufferFile {
long lastLog = System.currentTimeMillis();
long lastCount = 0;
+ boolean activeLimit = rateLimit;
while (!isCancelled() && (n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
count += n;
@@ -533,6 +537,21 @@ public class DownloadFile implements BufferFile {
currentSpeed = lastCount / ((now - lastLog) / 1000L);
lastLog = now;
lastCount = 0;
+
+ // Re-establish every few seconds whether screen is on or not
+ if(rateLimit) {
+ PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+ if(pm.isScreenOn()) {
+ activeLimit = true;
+ } else {
+ activeLimit = false;
+ }
+ }
+ }
+
+ // If screen is on and rateLimit is true, stop downloading from exhausting bandwidth
+ if(activeLimit) {
+ Thread.sleep(10L);
}
}
return count;
diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java
index ba4f34c9..f680becf 100644
--- a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java
+++ b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java
@@ -82,18 +82,16 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter {
for(MusicDirectory.Entry entry: playlist.getChildren()) {
DownloadFile file = new DownloadFile(context, entry, true);
String path = file.getCompleteFile().getPath();
- if(!cachedPlaylist.synced.contains(path)) {
- while(!file.isSaved() && !file.isFailedMax()) {
- file.downloadNow(musicService);
- if(!updated.contains(playlist.getName())) {
- updated.add(playlist.getName());
- }
+ while(!file.isSaved() && !file.isFailedMax()) {
+ file.downloadNow(musicService);
+ if(file.isSaved() && !updated.contains(playlist.getName())) {
+ updated.add(playlist.getName());
}
+ }
- // Add to cached path set if saved
- if(file.isSaved()) {
- cachedPlaylist.synced.add(path);
- }
+ // Add to cached path set if saved
+ if(file.isSaved() && !cachedPlaylist.synced.contains(path)) {
+ cachedPlaylist.synced.add(path);
}
origPathList.remove(path);
@@ -118,12 +116,13 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter {
Log.e(TAG, "Failed to get playlist " + id + " for " + serverName, e);
}
- if(updated.size() > 0) {
- SyncUtil.showSyncNotification(context, R.string.sync_new_playlists, SyncUtil.joinNames(updated));
- }
if(updated.size() > 0 || removed) {
SyncUtil.setSyncedPlaylists(context, instance, playlistList);
}
}
+
+ if(updated.size() > 0) {
+ SyncUtil.showSyncNotification(context, R.string.sync_new_playlists, SyncUtil.joinNames(updated));
+ }
}
}