aboutsummaryrefslogtreecommitdiff
path: root/src/github/daneren2005/dsub/service/Scrobbler.java
blob: 6772ec5d937bd2d6fa1ad6f0294a4a51945848f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package github.daneren2005.dsub.service;

import android.content.Context;
import android.util.Log;

import github.daneren2005.dsub.domain.PodcastEpisode;
import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.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 static final int FOUR_MINUTES = 4 * 60 * 1000;

    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;
        }
        
		// Ignore if online with no network access
		if(!Util.isOffline(context) && !Util.isNetworkConnected(context)) {
			return;
		}

		// Ignore podcasts
		if(song.getSong() instanceof PodcastEpisode) {
			return;
		}
		
		// Ignore songs which are under 30 seconds per Last.FM guidelines
		if(song.getDuration() != null && song.getDuration() > 0 && song.getDuration < 30) {
			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 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();
    }
}