diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-11-14 23:51:48 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-11-14 23:51:48 -0800 |
commit | d3993fc46d8efb9792a3cb64262b20a2b8e6b586 (patch) | |
tree | 2ba0fe58a9db67514c5116338adbf80b4ccbb0a8 /src/github | |
parent | 2ad1a9ace84137b18fd6f35174920ea659dde0a3 (diff) | |
parent | 9414e77ba152f815508479f5ffb4c5970c4ac117 (diff) | |
download | dsub-d3993fc46d8efb9792a3cb64262b20a2b8e6b586.tar.gz dsub-d3993fc46d8efb9792a3cb64262b20a2b8e6b586.tar.bz2 dsub-d3993fc46d8efb9792a3cb64262b20a2b8e6b586.zip |
Merge changes
Diffstat (limited to 'src/github')
6 files changed, 137 insertions, 11 deletions
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 5a7737de..29af6426 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -429,6 +429,11 @@ public class CachedMusicService implements MusicService { public int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception{ return musicService.processOfflineSyncs(context, progressListener); } + + @Override + public void setInstance(Integer instance) { + musicService.setInstance(instance); + } private String getCacheName(Context context, String name, String id) { String s = Util.getRestUrl(context, null) + id; diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index 8d204530..a572d7e2 100644 --- a/src/github/daneren2005/dsub/service/MusicService.java +++ b/src/github/daneren2005/dsub/service/MusicService.java @@ -146,4 +146,6 @@ public interface MusicService { void deleteBookmark(String id, Context context, ProgressListener progressListener) throws Exception; int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception; -}
\ No newline at end of file + + void setInstance(Integer instance); +} diff --git a/src/github/daneren2005/dsub/service/OfflineMusicService.java b/src/github/daneren2005/dsub/service/OfflineMusicService.java index ba798c09..802ac32d 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -660,6 +660,11 @@ public class OfflineMusicService extends RESTMusicService { public int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception{ throw new OfflineException("Offline scrobble cached can not be processes while in offline mode"); } + + @Override + public void setInstance(Integer instance) { + throw new OfflinceException("Offline servers only have one instance"); + } private void listFilesRecursively(File parent, List<File> children) { for (File file : FileUtil.listMediaFiles(parent)) { diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 22abacb1..3f3152c2 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -129,6 +129,7 @@ public class RESTMusicService implements MusicService { private String redirectFrom; private String redirectTo; private final ThreadSafeClientConnManager connManager; + private Integer instance; public RESTMusicService() { @@ -245,7 +246,7 @@ public class RESTMusicService implements MusicService { } private String getCachedIndexesFilename(Context context, String musicFolderId) { - String s = Util.getRestUrl(context, null) + musicFolderId; + String s = getRestUrl(context, null) + musicFolderId; return "indexes-" + Math.abs(s.hashCode()) + ".ser"; } @@ -596,7 +597,7 @@ public class RESTMusicService implements MusicService { return bitmap; } - String url = Util.getRestUrl(context, "getCoverArt"); + String url = getRestUrl(context, "getCoverArt"); InputStream in = null; try { @@ -640,7 +641,7 @@ public class RESTMusicService implements MusicService { @Override public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, CancellableTask task) throws Exception { - String url = Util.getRestUrl(context, "stream"); + String url = getRestUrl(context, "stream"); // Set socket read timeout. Note: The timeout increases as the offset gets larger. This is // to avoid the thrashing effect seen when offset is combined with transcoding/downsampling on the server. @@ -674,7 +675,7 @@ public class RESTMusicService implements MusicService { @Override public String getVideoUrl(int maxBitrate, Context context, String id) { - StringBuilder builder = new StringBuilder(Util.getRestUrl(context, "videoPlayer")); + StringBuilder builder = new StringBuilder(getRestUrl(context, "videoPlayer")); builder.append("&id=").append(id); builder.append("&maxBitRate=").append(maxBitrate); builder.append("&autoplay=true"); @@ -686,7 +687,7 @@ public class RESTMusicService implements MusicService { @Override public String getVideoStreamUrl(String format, int maxBitrate, Context context, String id) throws Exception { - StringBuilder builder = new StringBuilder(Util.getRestUrl(context, "stream")); + StringBuilder builder = new StringBuilder(getRestUrl(context, "stream")); builder.append("&id=").append(id); if(!"raw".equals(format)) { checkServerVersion(context, "1.9", "Video streaming not supported."); @@ -703,7 +704,7 @@ public class RESTMusicService implements MusicService { public String getHlsUrl(String id, int bitRate, Context context) throws Exception { checkServerVersion(context, "1.9", "HLS video streaming not supported."); - StringBuilder builder = new StringBuilder(Util.getRestUrl(context, "hls")); + StringBuilder builder = new StringBuilder(getRestUrl(context, "hls")); builder.append("&id=").append(id); if(bitRate > 0) { builder.append("&bitRate=").append(bitRate); @@ -1113,6 +1114,11 @@ public class RESTMusicService implements MusicService { return id; } + + @Override + public void setInstance(Integer instance) { + this.instance = instance; + } private Reader getReader(Context context, ProgressListener progressListener, String method, HttpParams requestParams) throws Exception { return getReader(context, progressListener, method, requestParams, Collections.<String>emptyList(), Collections.emptyList()); @@ -1130,7 +1136,7 @@ public class RESTMusicService implements MusicService { progressListener.updateProgress(R.string.service_connecting); } - String url = Util.getRestUrl(context, method); + String url = getRestUrl(context, method); return getReaderForURL(context, url, requestParams, parameterNames, parameterValues, progressListener); } @@ -1316,4 +1322,12 @@ public class RESTMusicService implements MusicService { NetworkInfo networkInfo = manager.getActiveNetworkInfo(); return networkInfo == null ? -1 : networkInfo.getType(); } + + private String getRestUrl(Context context, String method) { + if(instance == null) { + Util.getRestUrl(context, method); + } else { + Util.getRestUrl(context, method, instance); + } + } } diff --git a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java new file mode 100644 index 00000000..4073b1b3 --- /dev/null +++ b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java @@ -0,0 +1,94 @@ +/* + This file is part of Subsonic. + + Subsonic is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Subsonic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Subsonic. If not, see <http://www.gnu.org/licenses/>. + + Copyright 2009 (C) Sindre Mehus + */ + +package github.daneren2005.dsub.service.sync; + +import android.accounts.Account; +import android.annotation.TargetApi; +import android.content.AbstractThreadedSyncAdapter; +import android.content.ContentProviderClient; +import android.content.Context; +import android.content.SyncResult; +import android.os.Bundle; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.util.Log; + +import github.daneren2005.dsub.service.RESTMusicService; +import github.daneren2005.dsub.util.Util; + +/** + * Created by Scott on 9/6/13. + */ + +public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter { + private static final String TAG = SubsonicSyncAdapter.class.getSimpleName(); + protected RESTMusicService musicService = new RESTMusicService(); + + public SubsonicSyncAdapter(Context context, boolean autoInitialize) { + super(context, autoInitialize); + } + @TargetApi(14) + public SubsonicSyncSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) { + super(context, autoInitialize, allowParallelSyncs); + } + + @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"); + return; + } + + // 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); + } else { + Log.w(TAG, "Not running sync, not connected to wifi"); + } + } else { + executeSync(context); + } + } + + private void executeSync(Context context) { + String className = this.getClass().getSimpleName(); + Log.i(TAG, "Running sync for " + className); + long start = System.currentTimeMillis(); + try { + int servers = Util.getServerCount(context); + for(int i = 1; i <= servers; i++) { + musicService.setInstance(i); + onExecuteSync(context); + } + } 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) { + + } +} diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java index ce6ba914..9b1b01f0 100644 --- a/src/github/daneren2005/dsub/util/Util.java +++ b/src/github/daneren2005/dsub/util/Util.java @@ -329,11 +329,17 @@ public final class Util { } public static String getRestUrl(Context context, String method) { - StringBuilder builder = new StringBuilder(); - SharedPreferences prefs = getPreferences(context); - int instance = prefs.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1); + return getRestUrl(context, method, prefs, instance); + } + public static String getRestUrl(Context context, String method, int instance) { + SharedPreferences prefs = getPreferences(context); + return getRestUrl(context, method, prefs, instance); + } + public static String getRestUrl(Context context, String method, SharedPreferences prefs, int instance) { + StringBuilder builder = new StringBuilder(); + String serverUrl = prefs.getString(Constants.PREFERENCES_KEY_SERVER_URL + instance, null); String username = prefs.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null); String password = prefs.getString(Constants.PREFERENCES_KEY_PASSWORD + instance, null); |