From 04ee05fd4174c8e35e5462c8f3a7e4697214d39d Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 22 Jul 2014 17:53:49 -0700 Subject: #357 Add Rescan Server option for Madsonic 5.1+ --- src/github/daneren2005/dsub/domain/ServerInfo.java | 30 ++++++++++++ .../daneren2005/dsub/fragments/MainFragment.java | 24 ++++++++++ .../dsub/service/CachedMusicService.java | 7 ++- .../daneren2005/dsub/service/MusicService.java | 2 + .../dsub/service/OfflineMusicService.java | 7 ++- .../daneren2005/dsub/service/RESTMusicService.java | 23 +++------ .../dsub/service/parser/ScanStatusParser.java | 56 ++++++++++++++++++++++ 7 files changed, 131 insertions(+), 18 deletions(-) create mode 100644 src/github/daneren2005/dsub/service/parser/ScanStatusParser.java (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/domain/ServerInfo.java b/src/github/daneren2005/dsub/domain/ServerInfo.java index eb644cf7..0cce774e 100644 --- a/src/github/daneren2005/dsub/domain/ServerInfo.java +++ b/src/github/daneren2005/dsub/domain/ServerInfo.java @@ -152,6 +152,36 @@ public class ServerInfo implements Serializable { Version required = new Version(requiredVersion); return version.compareTo(required) >= 0; } + + public static int getServerType(Context context) { + return getServerType(context, Util.getActiveServer(context)); + } + public static int getServerType(Context context, int instance) { + if(Util.isOffline(context)) { + return 0; + } + + ServerInfo server = getServerInfo(context, instance); + if(server == null) { + return 0; + } + + return server.getRestType(); + } + + public static boolean isStockSubsonic(Context context) { + return isStockSubsonic(context, Util.getActiveServer(context)); + } + public static boolean isStockSubsonic(Context context, int instance) { + return getServerType(context, instance) == TYPE_SUBSONIC; + } + + public static boolean isMadsonic(Context context) { + return isMadsonic(context, Util.getActiveServer(context)); + } + public static boolean isMadsonic(Context context, int instance) { + return getServerType(context, instance) == TYPE_MADSONIC; + } private static String getCacheName(Context context, int instance) { return "server-" + Util.getRestUrl(context, null, instance, false).hashCode() + ".ser"; diff --git a/src/github/daneren2005/dsub/fragments/MainFragment.java b/src/github/daneren2005/dsub/fragments/MainFragment.java index 561e32c4..da0711ef 100644 --- a/src/github/daneren2005/dsub/fragments/MainFragment.java +++ b/src/github/daneren2005/dsub/fragments/MainFragment.java @@ -21,6 +21,7 @@ import android.widget.CheckBox; import android.widget.ListView; import android.widget.TextView; import github.daneren2005.dsub.R; +import github.daneren2005.dsub.domain.ServerInfo; import github.daneren2005.dsub.service.DownloadService; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.FileUtil; @@ -73,6 +74,10 @@ public class MainFragment extends SubsonicFragment { @Override public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { menuInflater.inflate(R.menu.main, menu); + + if(!ServerInfo.isMadsonic(context) || !UserUtil.isCurrentAdmin()) { + menu.setGroupVisible(R.id.madsonic, false); + } } @Override @@ -95,6 +100,9 @@ public class MainFragment extends SubsonicFragment { case R.id.menu_faq: showFAQDialog(); return true; + case R.id.menu_rescan: + rescanServer(); + return true; } return false; @@ -366,6 +374,22 @@ public class MainFragment extends SubsonicFragment { Util.showHTMLDialog(context, R.string.main_faq_title, R.string.main_faq_text); } + private void rescanServer() { + new LoadingTask(context, false) { + @Override + protected Void doInBackground() throws Throwable { + MusicService musicService = MusicServiceFactory.getMusicService(context); + musicService.startRescan(context, this); + return null; + } + + @Override + protected void done(Void value) { + Util.toast(context, R.string.main_scan_complete); + } + }.execute(); + } + private void getLogs() { try { final String version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 31280b19..86ad44f7 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -118,7 +118,12 @@ public class CachedMusicService implements MusicService { return result; } - @Override + @Override + public void startRescan(Context context, ProgressListener listener) throws Exception { + musicService.startRescan(context, listener); + } + + @Override public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception { checkSettingsChanged(context); if (refresh) { diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index cefd0c23..318873da 100644 --- a/src/github/daneren2005/dsub/service/MusicService.java +++ b/src/github/daneren2005/dsub/service/MusicService.java @@ -54,6 +54,8 @@ public interface MusicService { List getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception; + void startRescan(Context context, ProgressListener listener) throws Exception; + Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception; MusicDirectory getMusicDirectory(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception; diff --git a/src/github/daneren2005/dsub/service/OfflineMusicService.java b/src/github/daneren2005/dsub/service/OfflineMusicService.java index 05d17444..8a520c8a 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -272,7 +272,12 @@ public class OfflineMusicService implements MusicService { throw new OfflineException(ERRORMSG); } - @Override + @Override + public void startRescan(Context context, ProgressListener listener) throws Exception { + throw new OfflineException(ERRORMSG); + } + + @Override public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception { List artists = new ArrayList(); List albums = new ArrayList(); diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 15f06a3e..0129bf22 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -84,6 +84,7 @@ import github.daneren2005.dsub.service.parser.PlaylistsParser; import github.daneren2005.dsub.service.parser.PodcastChannelParser; import github.daneren2005.dsub.service.parser.PodcastEntryParser; import github.daneren2005.dsub.service.parser.RandomSongsParser; +import github.daneren2005.dsub.service.parser.ScanStatusParser; import github.daneren2005.dsub.service.parser.SearchResult2Parser; import github.daneren2005.dsub.service.parser.SearchResultParser; import github.daneren2005.dsub.service.parser.ShareParser; @@ -210,10 +211,10 @@ public class RESTMusicService implements MusicService { // Now check if still running boolean done = false; while(!done) { - reader = getReader(context, listener, "scanstatus", null); + reader = getReader(context, null, "scanstatus", null); try { - ScanStatus status = new ScanStatusParser(context).parse(reader); - if(status.isRunning()) { + boolean running = new ScanStatusParser(context).parse(reader, listener); + if(running) { // Don't run system ragged trying to query too much Thread.sleep(100L); } else { @@ -234,18 +235,6 @@ public class RESTMusicService implements MusicService { return cachedIndexes; } - // If manual refresh, try to start server scan for madsonic servers - /*if(refresh) { - Reader reader = getReader(context, progressListener, "startRescan", null); - try { - new ErrorParser(context).parse(reader); - } catch(Exception e) { - // Probably not madsonic, don't care - } finally { - Util.close(reader); - } - }*/ - long lastModified = (cachedIndexes == null || refresh) ? 0L : cachedIndexes.getLastModified(); List parameterNames = new ArrayList(); @@ -1548,7 +1537,9 @@ public class RESTMusicService implements MusicService { List parameterNames, List parameterValues, List
headers, ProgressListener progressListener, SilentBackgroundTask task) throws Exception { // Strip out sensitive information from log - Log.i(TAG, stripUrlInfo(url)); + if(url.indexOf("scanstatus") == -1) { + Log.i(TAG, stripUrlInfo(url)); + } SharedPreferences prefs = Util.getPreferences(context); int networkTimeout = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_NETWORK_TIMEOUT, "15000")); diff --git a/src/github/daneren2005/dsub/service/parser/ScanStatusParser.java b/src/github/daneren2005/dsub/service/parser/ScanStatusParser.java new file mode 100644 index 00000000..e26b0562 --- /dev/null +++ b/src/github/daneren2005/dsub/service/parser/ScanStatusParser.java @@ -0,0 +1,56 @@ +/* + 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 . + Copyright 2014 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.service.parser; + +import android.content.Context; +import org.xmlpull.v1.XmlPullParser; + +import java.io.Reader; + +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.util.ProgressListener; + +public class ScanStatusParser extends AbstractParser { + + public ScanStatusParser(Context context) { + super(context); + } + + public boolean parse(Reader reader, ProgressListener progressListener) throws Exception { + init(reader); + + Boolean started = null; + int eventType; + do { + eventType = nextParseEvent(); + if (eventType == XmlPullParser.START_TAG) { + String name = getElementName(); + if("status".equals(name)) { + started = getBoolean("started"); + + String msg = context.getResources().getString(R.string.parser_scan_count, getInteger("count")); + progressListener.updateProgress(msg); + } else if ("error".equals(name)) { + handleError(); + } + } + } while (eventType != XmlPullParser.END_DOCUMENT); + + validate(); + + return started != null && started; + } +} \ No newline at end of file -- cgit v1.2.3