aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/src/main/java/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java2
-rw-r--r--app/src/main/java/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java3
-rw-r--r--app/src/main/java/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java4
-rw-r--r--app/src/main/java/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java3
-rw-r--r--app/src/main/java/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java78
5 files changed, 59 insertions, 31 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java b/app/src/main/java/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java
index 8da83be1..bcb7b92f 100644
--- a/app/src/main/java/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java
+++ b/app/src/main/java/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java
@@ -49,7 +49,7 @@ public class MostRecentSyncAdapter extends SubsonicSyncAdapter {
}
@Override
- public void onExecuteSync(Context context, int instance) {
+ public void onExecuteSync(Context context, int instance) throws NetworkNotValidException {
try {
ArrayList<String> syncedList = SyncUtil.getSyncedMostRecent(context, instance);
MusicDirectory albumList = musicService.getAlbumList("newest", 20, 0, tagBrowsing, context, null);
diff --git a/app/src/main/java/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/app/src/main/java/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java
index 6c017472..cb3c3877 100644
--- a/app/src/main/java/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java
+++ b/app/src/main/java/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java
@@ -56,7 +56,7 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter {
}
@Override
- public void onExecuteSync(Context context, int instance) {
+ public void onExecuteSync(Context context, int instance) throws NetworkNotValidException {
String serverName = Util.getServerName(context, instance);
List<Playlist> remainder = null;
@@ -95,6 +95,7 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter {
DownloadFile file = new DownloadFile(context, entry, true);
String path = file.getCompleteFile().getPath();
while(!file.isSaved() && !file.isFailedMax()) {
+ throwIfNetworkInvalid();
file.downloadNow(musicService);
if(file.isSaved() && !updated.contains(playlist.getName())) {
updated.add(playlist.getName());
diff --git a/app/src/main/java/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java b/app/src/main/java/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java
index ce8e7a91..7afcad25 100644
--- a/app/src/main/java/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java
+++ b/app/src/main/java/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java
@@ -23,7 +23,6 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.util.Log;
-import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@@ -54,7 +53,7 @@ public class PodcastSyncAdapter extends SubsonicSyncAdapter {
}
@Override
- public void onExecuteSync(Context context, int instance) {
+ public void onExecuteSync(Context context, int instance) throws NetworkNotValidException {
ArrayList<SyncSet> podcastList = SyncUtil.getSyncedPodcasts(context, instance);
try {
@@ -81,6 +80,7 @@ public class PodcastSyncAdapter extends SubsonicSyncAdapter {
if(entry.getId() != null && "completed".equals(((PodcastEpisode)entry).getStatus()) && !existingEpisodes.contains(entry.getId())) {
DownloadFile file = new DownloadFile(context, entry, false);
while(!file.isCompleteFileAvailable() && !file.isFailedMax()) {
+ throwIfNetworkInvalid();
file.downloadNow(musicService);
}
// Only add if actualy downloaded correctly
diff --git a/app/src/main/java/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java b/app/src/main/java/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java
index cf985227..0af8886b 100644
--- a/app/src/main/java/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java
+++ b/app/src/main/java/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java
@@ -20,7 +20,6 @@
package github.daneren2005.dsub.service.sync;
import android.annotation.TargetApi;
-import android.app.Notification;
import android.content.Context;
import android.util.Log;
@@ -50,7 +49,7 @@ public class StarredSyncAdapter extends SubsonicSyncAdapter {
}
@Override
- public void onExecuteSync(Context context, int instance) {
+ public void onExecuteSync(Context context, int instance) throws NetworkNotValidException {
try {
ArrayList<String> syncedList = new ArrayList<String>();
MusicDirectory starredList = musicService.getStarredList(context, null);
diff --git a/app/src/main/java/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java b/app/src/main/java/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java
index 661f126d..4879d032 100644
--- a/app/src/main/java/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java
+++ b/app/src/main/java/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java
@@ -65,39 +65,54 @@ public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter {
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
- ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = manager.getActiveNetworkInfo();
-
- // Don't try to sync if no network!
- if(networkInfo == null || !networkInfo.isConnected() || Util.isOffline(context)) {
- Log.w(TAG, "Not running sync, not connected to network");
+ String invalidMessage = isNetworkValid();
+ if(invalidMessage != null) {
+ Log.w(TAG, "Not running sync: " + invalidMessage);
return;
}
-
+
// Make sure battery > x% or is charging
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, intentFilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
- if(status != BatteryManager.BATTERY_STATUS_CHARGING && status != BatteryManager.BATTERY_STATUS_FULL) {
+ if (status != BatteryManager.BATTERY_STATUS_CHARGING && status != BatteryManager.BATTERY_STATUS_FULL) {
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
-
- if((level / (float)scale) < 0.15) {
+
+ if ((level / (float) scale) < 0.15) {
Log.w(TAG, "Not running sync, battery too low");
return;
}
}
+ executeSync(context);
+ }
+
+ private String isNetworkValid() {
+ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+ NetworkInfo networkInfo = manager.getActiveNetworkInfo();
+
+ // Don't try to sync if no network!
+ if(networkInfo == null || !networkInfo.isConnected() || Util.isOffline(context)) {
+ return "Not connected to any network";
+ }
+
// Check if user wants to only sync on wifi
SharedPreferences prefs = Util.getPreferences(context);
if(prefs.getBoolean(Constants.PREFERENCES_KEY_SYNC_WIFI, true)) {
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
- executeSync(context);
+ return null;
} else {
- Log.w(TAG, "Not running sync, not connected to wifi");
+ return "Not connected to WIFI";
}
} else {
- executeSync(context);
+ return null;
+ }
+ }
+ protected void throwIfNetworkInvalid() throws NetworkNotValidException {
+ String invalidMessage = isNetworkValid();
+ if(invalidMessage != null) {
+ throw new NetworkNotValidException(invalidMessage);
}
}
@@ -106,32 +121,39 @@ public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter {
Log.i(TAG, "Running sync for " + className);
long start = System.currentTimeMillis();
int servers = Util.getServerCount(context);
- for(int i = 1; i <= servers; i++) {
- try {
- if(isValidServer(context, i) && Util.isSyncEnabled(context, i)) {
- tagBrowsing = Util.isTagBrowsing(context, i);
- musicService.setInstance(i);
- onExecuteSync(context, i);
- } else {
- Log.i(TAG, "Skipped sync for " + i);
+ try {
+ for (int i = 1; i <= servers; i++) {
+ try {
+ throwIfNetworkInvalid();
+
+ if (isValidServer(context, i) && Util.isSyncEnabled(context, i)) {
+ tagBrowsing = Util.isTagBrowsing(context, i);
+ musicService.setInstance(i);
+ onExecuteSync(context, i);
+ } else {
+ Log.i(TAG, "Skipped sync for " + i);
+ }
+ } catch (Exception e) {
+ Log.e(TAG, "Failed sync for " + className + "(" + i + ")", e);
}
- } catch(Exception e) {
- Log.e(TAG, "Failed sync for " + className + "(" + i + ")", e);
}
+ } catch (NetworkNotValidException e) {
+ Log.e(TAG, "Stopped sync due to network loss", e);
}
Log.i(TAG, className + " executed in " + (System.currentTimeMillis() - start) + " ms");
}
- public void onExecuteSync(Context context, int instance) {
+ public void onExecuteSync(Context context, int instance) throws NetworkNotValidException {
}
- protected boolean downloadRecursively(List<String> paths, MusicDirectory parent, Context context, boolean save) throws Exception {
+ protected boolean downloadRecursively(List<String> paths, MusicDirectory parent, Context context, boolean save) throws Exception,NetworkNotValidException {
boolean downloaded = false;
for (MusicDirectory.Entry song: parent.getChildren(false, true)) {
if (!song.isVideo()) {
DownloadFile file = new DownloadFile(context, song, save);
while(!(save && file.isSaved() || !save && file.isCompleteFileAvailable()) && !file.isFailedMax()) {
+ throwIfNetworkInvalid();
file.downloadNow(musicService);
if(!file.isFailed()) {
downloaded = true;
@@ -171,4 +193,10 @@ public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter {
String url = Util.getRestUrl(context, "null", instance, false);
return !(url.contains("demo.subsonic.org") || url.contains("yourhost"));
}
+
+ public class NetworkNotValidException extends Throwable {
+ public NetworkNotValidException(String reason) {
+ super("Not running sync: " + reason);
+ }
+ }
}