From ec8f068abc0d3fa3b14ca173afcacc1f431e36e3 Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Tue, 15 Apr 2014 14:30:50 -0700 Subject: Add rate limiting for sync service downloading A background sync operation can make the system unusable as every single network operation will basically never finish until the SyncAdapter is done downloading. --- src/github/daneren2005/dsub/service/DownloadFile.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') 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; -- cgit v1.2.3 From 500a1031b9f6b98ab85cb962d4dab706a3938981 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 15 Apr 2014 19:51:22 -0700 Subject: Fix PlaylistAdapter not picking up deleted cache after first pass --- .../dsub/service/sync/PlaylistSyncAdapter.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java index ba4f34c9..5c518d7e 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); -- cgit v1.2.3 From 016a0b31da1ceb42a4422023ba1ffe9ff360ab39 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 15 Apr 2014 22:04:56 -0700 Subject: Only show notification after entire playlist sync is done --- src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java index 5c518d7e..f680becf 100644 --- a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java @@ -116,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)); + } } } -- cgit v1.2.3