aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2013-11-23 15:14:21 -0800
committerScott Jackson <daneren2005@gmail.com>2013-11-23 15:14:21 -0800
commit72ebeec2943211c3d88a1bd4dc441870680a5261 (patch)
tree315693e38bfdc2427070534d5d42fec6698f4645 /src
parente7039a7e10c3efcb5d637bbbaa0111b092a4e214 (diff)
parentcc447a8331335fa98003c57c439865c76b6ec2d2 (diff)
downloaddsub-72ebeec2943211c3d88a1bd4dc441870680a5261.tar.gz
dsub-72ebeec2943211c3d88a1bd4dc441870680a5261.tar.bz2
dsub-72ebeec2943211c3d88a1bd4dc441870680a5261.zip
Merge branch 'SyncAdapter' of https://github.com/daneren2005/Subsonic into SyncAdapter
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/service/DownloadFile.java14
-rw-r--r--src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java29
-rw-r--r--src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java26
-rw-r--r--src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java20
4 files changed, 74 insertions, 15 deletions
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() {
diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java
index bb07be47..0047b997 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 <http://www.gnu.org/licenses/>.
Copyright 2009 (C) Sindre Mehus
- */
+*/
package github.daneren2005.dsub.service.sync;
@@ -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 PlaylistSyncAdapter extends AbstractThreadedSyncAdapter {
+public class PlaylistSyncAdapter extends SubsonicSyncAdapter {
public PlaylistSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
@@ -40,8 +44,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<Integer> 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);
+ while(!file.isSaved() && !file.isFailedMax()) {
+ file.downloadNow();
+ }
+ }
+ }
}
}
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<Integer> podcastList = FileUtil.deserialize(context, podcastListFile, ArrayList.class);
+ for(int i = 0; i < podcastList.size(); i++) {
+ String id = Integer.toString(podcastList.get(i));
+ List<PodcastChannel> 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();
+ }
+ }
+ }
}
}
diff --git a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java
index 71b7ae04..106049bb 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);
@@ -85,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) {
}
}