From 6aa48f1b3faece3d949cc3980ac405a557bcc9c1 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 14 Jun 2013 20:47:39 -0700 Subject: Abstract out search string parsing, use when playing offline mode songs in online mode --- .../daneren2005/dsub/service/OfflineMusicService.java | 19 +++---------------- .../daneren2005/dsub/service/RESTMusicService.java | 14 +++++++++++++- .../src/github/daneren2005/dsub/util/Util.java | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java b/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java index bda112ff..1878fecf 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -443,25 +443,12 @@ public class OfflineMusicService extends RESTMusicService { SharedPreferences.Editor offlineEditor = offline.edit(); if(id.indexOf(cacheLocn) != -1) { - String scrobbleSearchCriteria = id.replace(cacheLocn, ""); - if(scrobbleSearchCriteria.startsWith("/")) { - scrobbleSearchCriteria = scrobbleSearchCriteria.substring(1); - } - - scrobbleSearchCriteria = scrobbleSearchCriteria.replace(".complete", "").replace(".partial", ""); - int index = scrobbleSearchCriteria.lastIndexOf("."); - scrobbleSearchCriteria = index == -1 ? scrobbleSearchCriteria : scrobbleSearchCriteria.substring(0, index); - String[] details = scrobbleSearchCriteria.split("/"); - - //last.fm only uses artist and track title so broaden the search by just using those. doesn't matter if it find the track on a different album - String artist = "artist:\"" + details[0] + "\""; - String title = details[details.length - 1]; - title = "title:\"" + title.substring(title.indexOf('-') + 1) + "\""; - - scrobbleSearchCriteria = artist + " AND " + title; + String scrobbleSearchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn); offlineEditor.putString(Constants.OFFLINE_SCROBBLE_SEARCH + scrobbles, scrobbleSearchCriteria); + offlineEditor.remove(Constants.OFFLINE_SCROBBLE_ID + scrobbles); } else { offlineEditor.putString(Constants.OFFLINE_SCROBBLE_ID + scrobbles, id); + offlineEditor.remove(Constants.OFFLINE_SCROBBLE_SEARCH + scrobbles); } offlineEditor.putLong(Constants.OFFLINE_SCROBBLE_TIME + scrobbles, System.currentTimeMillis()); diff --git a/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java b/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java index 53eccf71..249efc10 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -491,7 +491,19 @@ public class RESTMusicService implements MusicService { @Override public void scrobble(String id, boolean submission, Context context, ProgressListener progressListener) throws Exception { - scrobble(id, submission, 0, context, progressListener); + SharedPreferences prefs = Util.getPreferences(context); + String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); + + if(id.indexOf(cacheLocn) != -1 && submission) { + String scrobbleSearchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn); + SearchCritera critera = new SearchCritera(scrobbleSearchCriteria, 0, 0, 1); + SearchResult result = searchNew(critera, context, progressListener); + if(result.getSongs().size() == 1){ + scrobble(result.getSongs().get(0).getId(), true, 0, context, progressListener); + } + } else { + scrobble(id, submission, 0, context, progressListener); + } } public void scrobble(String id, boolean submission, long time, Context context, ProgressListener progressListener) throws Exception { diff --git a/subsonic-android/src/github/daneren2005/dsub/util/Util.java b/subsonic-android/src/github/daneren2005/dsub/util/Util.java index 76bf6a04..25e7c690 100644 --- a/subsonic-android/src/github/daneren2005/dsub/util/Util.java +++ b/subsonic-android/src/github/daneren2005/dsub/util/Util.java @@ -340,6 +340,25 @@ public final class Util { SharedPreferences offline = getOfflineSync(context); return offline.getInt(Constants.OFFLINE_SCROBBLE_COUNT, 0); } + + public static String parseOfflineIDSearch(Context context, String id, String cacheLocation) { + String name = id.replace(cacheLocation, ""); + if(name.startsWith("/")) { + name = name.substring(1); + } + name = name.replace(".complete", "").replace(".partial", ""); + int index = name.lastIndexOf("."); + name = index == -1 ? name : name.substring(0, index); + String[] details = name.split("/"); + + String artist = "artist:\"" + details[0] + "\""; + String title = details[details.length - 1]; + title = "title:\"" + title.substring(title.indexOf('-') + 1) + "\""; + + name = artist + " AND " + title; + + return name; + } public static String getContentType(HttpEntity entity) { if (entity == null || entity.getContentType() == null) { -- cgit v1.2.3