2015-07-26 18:15:07 +02:00
|
|
|
package org.moire.ultrasonic.service;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
import org.moire.ultrasonic.data.ActiveServerProvider;
|
2015-07-26 18:15:07 +02:00
|
|
|
import org.moire.ultrasonic.util.Util;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrobbles played songs to Last.fm.
|
|
|
|
*
|
|
|
|
* @author Sindre Mehus
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
2013-12-04 07:36:02 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-09-18 09:37:19 +02:00
|
|
|
if (song == null || !ActiveServerProvider.Companion.isScrobblingEnabled(context))
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
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(String.format("Scrobble %s", song))
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
MusicService service = MusicServiceFactory.getMusicService(context);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
service.scrobble(id, submission, context, null);
|
|
|
|
Log.i(TAG, String.format("Scrobbled '%s' for %s", submission ? "submission" : "now playing", song));
|
|
|
|
}
|
|
|
|
catch (Exception x)
|
|
|
|
{
|
|
|
|
Log.i(TAG, String.format("Failed to scrobble'%s' for %s", submission ? "submission" : "now playing", song), x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
|
|
|
}
|
2012-02-26 21:25:13 +01:00
|
|
|
}
|