aboutsummaryrefslogtreecommitdiff
path: root/src/github/daneren2005
diff options
context:
space:
mode:
Diffstat (limited to 'src/github/daneren2005')
-rw-r--r--src/github/daneren2005/dsub/service/DownloadService.java37
-rw-r--r--src/github/daneren2005/dsub/service/Scrobbler.java85
2 files changed, 77 insertions, 45 deletions
diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java
index 9d454817..127df768 100644
--- a/src/github/daneren2005/dsub/service/DownloadService.java
+++ b/src/github/daneren2005/dsub/service/DownloadService.java
@@ -566,7 +566,9 @@ public class DownloadService extends Service {
public synchronized void clear(boolean serialize) {
// Delete podcast if fully listened to
- boolean cutoff = isPastCutoff();
+ int position = getPlayerPosition();
+ int duration = getPlayerDuration();
+ boolean cutoff = isPastCutoff(position, duration);
if(currentPlaying != null && currentPlaying.getSong() instanceof PodcastEpisode) {
if(cutoff) {
currentPlaying.delete();
@@ -584,6 +586,9 @@ public class DownloadService extends Service {
// Check if we should be adding a new bookmark here
checkAddBookmark();
}
+ if(currentPlaying != null) {
+ scrobbler.conditionalScrobble(this, currentPlaying, position, duration);
+ }
reset();
downloadList.clear();
@@ -898,12 +903,20 @@ public class DownloadService extends Service {
}
// Delete podcast if fully listened to
+ int position = getPlayerPosition();
+ int duration = getPlayerDuration();
+ boolean cutoff = isPastCutoff(position, duration);
if(currentPlaying != null && currentPlaying.getSong() instanceof PodcastEpisode) {
- if(isPastCutoff()) {
+ if(cutoff) {
toDelete.add(currentPlaying);
}
}
- clearCurrentBookmark();
+ if(cutoff) {
+ clearCurrentBookmark(true);
+ }
+ if(currentPlaying != null) {
+ scrobbler.conditionalScrobble(this, currentPlaying, position, duration);
+ }
int index = getCurrentPlayingIndex();
int nextPlayingIndex = getNextPlayingIndex();
@@ -1796,35 +1809,37 @@ public class DownloadService extends Service {
}
private boolean isPastCutoff() {
- int duration = getPlayerDuration();
+ return isPastCutoff(getPlayerPosition(), getPlayerDuration());
+ }
+ private boolean isPastCutoff(int position, int duration) {
int cutoffPoint = (int) (duration * DELETE_CUTOFF);
- return duration > 0 && getPlayerPosition() > cutoffPoint;
+ return duration > 0 && position > cutoffPoint;
}
private void clearCurrentBookmark() {
clearCurrentBookmark(false);
}
- private void clearCurrentBookmark(boolean delete) {
+ private void clearCurrentBookmark(boolean checkDelete) {
// If current is null, nothing to do
if(currentPlaying == null) {
return;
}
- clearCurrentBookmark(currentPlaying.getSong(), delete);
+ clearCurrentBookmark(currentPlaying.getSong(), checkDelete);
}
- private void clearCurrentBookmark(final MusicDirectory.Entry entry, boolean delete) {
+ private void clearCurrentBookmark(final MusicDirectory.Entry entry, boolean checkDelete) {
// If no bookmark, move on
if(entry.getBookmark() == null) {
return;
}
// If delete is not specified, check position
- if(!delete) {
- delete = isPastCutoff();
+ if(!checkDelete) {
+ checkDelete = isPastCutoff();
}
// If supposed to delete
- if(delete) {
+ if(checkDelete) {
new SilentBackgroundTask<Void>(this) {
@Override
public Void doInBackground() throws Throwable {
diff --git a/src/github/daneren2005/dsub/service/Scrobbler.java b/src/github/daneren2005/dsub/service/Scrobbler.java
index 9958ff9e..1f8538c9 100644
--- a/src/github/daneren2005/dsub/service/Scrobbler.java
+++ b/src/github/daneren2005/dsub/service/Scrobbler.java
@@ -14,17 +14,28 @@ import github.daneren2005.dsub.util.Util;
* @version $Id$
*/
public class Scrobbler {
+ private static final String TAG = Scrobbler.class.getSimpleName();
+ private static final int FOUR_MINUTES = 4 * 60 * 1000;
- private static final String TAG = Scrobbler.class.getSimpleName();
+ private String lastSubmission;
+ private String lastNowPlaying;
- private String lastSubmission;
- private String lastNowPlaying;
+ public void conditionalScrobble(Context context, DownloadFile song, int playerPosition, int duration) {
+ // More than 4 minutes
+ if(playerPosition > FOUR_MINUTES) {
+ scrobble(context, song, true);
+ }
+ // More than 50% played
+ else if(duration > 0 && playerPosition > (duration / 2)) {
+ scrobble(context, song, true);
+ }
+ }
+
+ public void scrobble(final Context context, final DownloadFile song, final boolean submission) {
+ if (song == null || !Util.isScrobblingEnabled(context)) {
+ return;
+ }
- public void scrobble(final Context context, final DownloadFile song, final boolean submission) {
- if (song == null || !Util.isScrobblingEnabled(context)) {
- return;
- }
-
// Ignore if online with no network access
if(!Util.isOffline(context) && !Util.isNetworkConnected(context)) {
return;
@@ -35,34 +46,40 @@ public class Scrobbler {
return;
}
- final String id = song.getSong().getId();
+ // Ignore songs which are under 30 seconds per Last.FM guidelines
+ Integer duration = song.getSong().getDuration();
+ if(duration != null && duration > 0 && duration < 30) {
+ return;
+ }
- // Avoid duplicate registrations.
- if (submission && id.equals(lastSubmission)) {
- return;
- }
- if (!submission && id.equals(lastNowPlaying)) {
- return;
- }
+ final String id = song.getSong().getId();
- if (submission) {
- lastSubmission = id;
- } else {
- lastNowPlaying = id;
- }
+ // Avoid duplicate registrations.
+ if (submission && id.equals(lastSubmission)) {
+ return;
+ }
+ if (!submission && id.equals(lastNowPlaying)) {
+ return;
+ }
+
+ if (submission) {
+ lastSubmission = id;
+ } else {
+ lastNowPlaying = id;
+ }
- new SilentBackgroundTask<Void>(context) {
- @Override
- protected Void doInBackground() {
- 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);
- }
+ new SilentBackgroundTask<Void>(context) {
+ @Override
+ protected Void doInBackground() {
+ 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);
+ }
return null;
- }
- }.execute();
- }
+ }
+ }.execute();
+ }
}