diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-04-19 13:20:46 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-04-19 13:20:46 -0700 |
commit | ade2ba555cc7ef5a5173f75f4d67b2c5db495795 (patch) | |
tree | d54f0a2f5d29a55dee9f671bf09e19b7b1a155ff /src | |
parent | 27c52a24168c715ccc3809fe158e0e3acec5a9a6 (diff) | |
parent | 016a0b31da1ceb42a4422023ba1ffe9ff360ab39 (diff) | |
download | dsub-ade2ba555cc7ef5a5173f75f4d67b2c5db495795.tar.gz dsub-ade2ba555cc7ef5a5173f75f4d67b2c5db495795.tar.bz2 dsub-ade2ba555cc7ef5a5173f75f4d67b2c5db495795.zip |
Merge branch 'RateL' of https://github.com/daneren2005/Subsonic
Diffstat (limited to 'src')
-rw-r--r-- | src/github/daneren2005/dsub/service/DownloadFile.java | 19 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java | 25 |
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));
+ }
}
}
|