diff options
Diffstat (limited to 'subsonic-android/src/github/daneren2005/subphonic/service/Scrobbler.java')
-rw-r--r-- | subsonic-android/src/github/daneren2005/subphonic/service/Scrobbler.java | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/subsonic-android/src/github/daneren2005/subphonic/service/Scrobbler.java b/subsonic-android/src/github/daneren2005/subphonic/service/Scrobbler.java new file mode 100644 index 00000000..de0e9980 --- /dev/null +++ b/subsonic-android/src/github/daneren2005/subphonic/service/Scrobbler.java @@ -0,0 +1,52 @@ +package github.daneren2005.subphonic.service; + +import android.content.Context; +import android.util.Log; +import github.daneren2005.subphonic.util.Util; + +/** + * Scrobbles played songs to Last.fm. + * + * @author Sindre Mehus + * @version $Id$ + */ +public class Scrobbler { + + private static final String TAG = Scrobbler.class.getSimpleName(); + + private String lastSubmission; + private String lastNowPlaying; + + public void scrobble(final Context context, final DownloadFile song, final boolean submission) { + if (song == null || !Util.isScrobblingEnabled(context)) { + return; + } + final String id = song.getSong().getId(); + + // Avoid duplicate registrations. + if (submission && id.equals(lastSubmission)) { + return; + } + if (!submission && id.equals(lastNowPlaying)) { + return; + } + if (submission) { + lastSubmission = id; + } else { + lastNowPlaying = id; + } + + new Thread("Scrobble " + song) { + @Override + public void run() { + MusicService service = MusicServiceFactory.getMusicService(context); + try { + service.scrobble(id, submission, context, null); + Log.i(TAG, "Scrobbled '" + (submission ? "submission" : "now playing") + "' for " + song); + } catch (Exception x) { + Log.i(TAG, "Failed to scrobble'" + (submission ? "submission" : "now playing") + "' for " + song, x); + } + } + }.start(); + } +} |