diff options
-rw-r--r-- | src/github/daneren2005/dsub/service/DownloadFile.java | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/github/daneren2005/dsub/service/DownloadFile.java b/src/github/daneren2005/dsub/service/DownloadFile.java index 9b7dd45d..9ddabfa4 100644 --- a/src/github/daneren2005/dsub/service/DownloadFile.java +++ b/src/github/daneren2005/dsub/service/DownloadFile.java @@ -67,6 +67,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; @@ -150,10 +151,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 { @@ -523,6 +526,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; @@ -534,6 +538,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; |