From 965b83f9227a53eb26142f14f35664e20628224e Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Fri, 15 Nov 2013 16:08:20 -0800 Subject: Add battery check for sync --- .../dsub/service/sync/SubsonicSyncAdapter.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java index 71b7ae04..ceee0e80 100644 --- a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java @@ -24,8 +24,11 @@ import android.annotation.TargetApi; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SyncResult; +import android.os.BatteryManager; import android.os.Bundle; import android.net.ConnectivityManager; import android.net.NetworkInfo; @@ -63,6 +66,19 @@ public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter { Log.w(TAG, "Not running sync, not connected to network"); return; } + + // Make sure battery > x% or is charging + IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + Intent batteryStatus = context.registerReceiver(null, intentFilter); + if(batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1) != BatteryManager.BATTERY_STATUS_CHARGING) { + int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); + int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); + + if((level / (float)scale) > 0.15) { + Log.w(TAG, "Not running sync, battery too low"); + return; + } + } // Check if user wants to only sync on wifi SharedPreferences prefs = Util.getPreferences(context); -- cgit v1.2.3 From 085d8ce2f17d4f2321c97dee5f3909b0f9668b8e Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Thu, 21 Nov 2013 16:47:29 -0800 Subject: Added instance to onExecuteSync --- src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java index ceee0e80..106049bb 100644 --- a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java @@ -101,14 +101,14 @@ public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter { int servers = Util.getServerCount(context); for(int i = 1; i <= servers; i++) { musicService.setInstance(i); - onExecuteSync(context); + onExecuteSync(context, i); } } catch(Exception e) { Log.e(TAG, "Failed sync for " + className, e); } Log.i(TAG, className + " executed in " + (System.currentTimeMillis() - start) + " ms"); } - public void onExecuteSync(Context context) { + public void onExecuteSync(Context context, int instance) { } } -- cgit v1.2.3 From c6e853e48d381f06c6555a41db11837dd6fa1240 Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Thu, 21 Nov 2013 17:05:21 -0800 Subject: Added playlist sync logic --- .../dsub/service/sync/PlaylistSyncAdapter.java | 28 +++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java index bb07be47..e689f0c9 100644 --- a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java @@ -15,7 +15,7 @@ along with Subsonic. If not, see . Copyright 2009 (C) Sindre Mehus - */ +*/ package github.daneren2005.dsub.service.sync; @@ -26,12 +26,15 @@ import android.content.ContentProviderClient; import android.content.Context; import android.content.SyncResult; import android.os.Bundle; +import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.domain.Playlist; +import github.daneren2005.dsub.util.Util; /** * Created by Scott on 8/28/13. - */ +*/ -public class PlaylistSyncAdapter extends AbstractThreadedSyncAdapter { +public class PlaylistSyncAdapter extends SubsonicSyncAdapter { public PlaylistSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); } @@ -40,8 +43,21 @@ public class PlaylistSyncAdapter extends AbstractThreadedSyncAdapter { super(context, autoInitialize, allowParallelSyncs); } - @Override - public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { - + @Override + public void onExecuteSync(Context context, int instance) { + String serverName = Util.getServerName(context, instance); + String playlistListFile = "sync-playlist-" + (Util.getRestUrl(context, null, instance)).hasCode() + ".ser"; + List playlistList = FileUtil.deserialize(context, playlistListFile, ArrayList.class); + for(int i = 0; i < playlistList.size(); i++) { + String id = Integer.toString(playlistList.get(i)); + MusicDirectory playlist = musicService.getPlaylist(true, id, serverName, context, null); + + for(MusicDirectory.Entry entry: playlist.getChildren()) { + DownloadFile file = new DownloadFile(context, entry, true); + if(!file.isSaved()) { + // TODO: Figure something out to download these one at a time! + } + } + } } } -- cgit v1.2.3 From d560faa5d53c6fb493e75d8017eae8956cb7a242 Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Fri, 22 Nov 2013 16:47:36 -0800 Subject: Added a DownloadFile.downloadNow method --- src/github/daneren2005/dsub/service/DownloadFile.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/DownloadFile.java b/src/github/daneren2005/dsub/service/DownloadFile.java index c2cffdef..51649b62 100644 --- a/src/github/daneren2005/dsub/service/DownloadFile.java +++ b/src/github/daneren2005/dsub/service/DownloadFile.java @@ -99,13 +99,21 @@ public class DownloadFile { } public synchronized void download() { - FileUtil.createDirectoryForParent(saveFile); + preDownload(); + downloadTask.start(); + } + public synchronized void downloadNow() { + preDownload(); + downloadTask.execute(); + + } + private void preDownload() { + FileUtil.createDirectoryForParent(saveFile); failedDownload = false; if(!partialFile.exists()) { bitRate = Util.getMaxBitrate(context); } - downloadTask = new DownloadTask(); - downloadTask.start(); + downloadTask = new DownloadTask(); } public synchronized void cancelDownload() { -- cgit v1.2.3 From 30c6d3853e1d971a86f83604f8f898a291efa9e0 Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Fri, 22 Nov 2013 16:52:59 -0800 Subject: Download new songs in playlist in order --- src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java index e689f0c9..0047b997 100644 --- a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java @@ -28,6 +28,7 @@ import android.content.SyncResult; import android.os.Bundle; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.Playlist; +import github.daneren2005.dsub.service.DownloadFile; import github.daneren2005.dsub.util.Util; /** @@ -54,8 +55,8 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter { for(MusicDirectory.Entry entry: playlist.getChildren()) { DownloadFile file = new DownloadFile(context, entry, true); - if(!file.isSaved()) { - // TODO: Figure something out to download these one at a time! + while(!file.isSaved() && !file.isFailedMax()) { + file.downloadNow(); } } } -- cgit v1.2.3 From cc447a8331335fa98003c57c439865c76b6ec2d2 Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Fri, 22 Nov 2013 16:59:45 -0800 Subject: Added basic podcast sync logic --- .../dsub/service/sync/PodcastSyncAdapter.java | 26 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java index 9a312900..6de7c74e 100644 --- a/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java @@ -26,12 +26,16 @@ import android.content.ContentProviderClient; import android.content.Context; import android.content.SyncResult; import android.os.Bundle; +import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.domain.Playlist; +import github.daneren2005.dsub.service.DownloadFile; +import github.daneren2005.dsub.util.Util; /** * Created by Scott on 8/28/13. */ -public class PodcastSyncAdapter extends AbstractThreadedSyncAdapter { +public class PodcastSyncAdapter extends SubsonicSyncAdapter { public PodcastSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); } @@ -40,8 +44,22 @@ public class PodcastSyncAdapter extends AbstractThreadedSyncAdapter { super(context, autoInitialize, allowParallelSyncs); } - @Override - public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { - + @Override + public void onExecuteSync(Context context, int instance) { + String serverName = Util.getServerName(context, instance); + String podcastListFile = "sync-podcast-" + (Util.getRestUrl(context, null, instance)).hasCode() + ".ser"; + List podcastList = FileUtil.deserialize(context, podcastListFile, ArrayList.class); + for(int i = 0; i < podcastList.size(); i++) { + String id = Integer.toString(podcastList.get(i)); + List podcasts = musicService.getPodcastEpisodes(true, id, context, null); + + // TODO: Only grab most recent podcasts! + for(PodcastChannel entry: podcasts) { + DownloadFile file = new DownloadFile(context, entry, true); + while(!file.isSaved() && !file.isFailedMax()) { + file.downloadNow(); + } + } + } } } -- cgit v1.2.3