Search in sources :

Example 1 with Result

use of com.softartdev.lastfm.Result in project scroball by peterjosling.

the class Scrobbler method updateNowPlaying.

public void updateNowPlaying(Track track) {
    if (!client.isAuthenticated()) {
        Log.d(TAG, "Skipping now playing update, not logged in.");
        return;
    }
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    if (!isConnected) {
        return;
    }
    client.updateNowPlaying(track, message -> {
        LastfmClient.Result result = (LastfmClient.Result) message.obj;
        int errorCode = result.errorCode();
        if (LastfmClient.isAuthenticationError(errorCode)) {
            notificationManager.notifyAuthError();
            ScroballApplication.getEventBus().post(AuthErrorEvent.create(errorCode));
        }
        return true;
    });
}
Also used : NetworkInfo(android.net.NetworkInfo) Result(com.softartdev.lastfm.Result)

Example 2 with Result

use of com.softartdev.lastfm.Result in project scroball by peterjosling.

the class Scrobbler method fetchTrackDurationAndSubmit.

public void fetchTrackDurationAndSubmit(final PlaybackItem playbackItem) {
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    if (!isConnected || !client.isAuthenticated()) {
        Log.d(TAG, "Offline or unauthenticated, can't fetch track duration. Saving for later.");
        queuePendingPlaybackItem(playbackItem);
        return;
    }
    Track track = playbackItem.getTrack();
    client.getTrackInfo(track, message -> {
        if (message.obj == null) {
            Result result = Caller.getInstance().getLastResult();
            int errorCode = 1;
            if (result != null) {
                errorCode = result.getErrorCode();
            }
            if (errorCode == 6) {
                Log.d(TAG, "Track not found, cannot scrobble.");
            // TODO prompt user to scrobble anyway
            } else {
                if (LastfmClient.isTransientError(errorCode)) {
                    Log.d(TAG, "Failed to fetch track duration, saving for later.");
                    queuePendingPlaybackItem(playbackItem);
                }
                if (LastfmClient.isAuthenticationError(errorCode)) {
                    notificationManager.notifyAuthError();
                    ScroballApplication.getEventBus().post(AuthErrorEvent.create(errorCode));
                }
            }
            return true;
        }
        Track updatedTrack = (Track) message.obj;
        playbackItem.updateTrack(updatedTrack);
        Log.d(TAG, String.format("Track info updated: %s", playbackItem));
        submit(playbackItem);
        return true;
    });
}
Also used : NetworkInfo(android.net.NetworkInfo) Result(com.softartdev.lastfm.Result)

Example 3 with Result

use of com.softartdev.lastfm.Result in project scroball by peterjosling.

the class Scrobbler method scrobblePending.

public void scrobblePending() {
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    boolean tracksPending = !(pending.isEmpty() && pendingPlaybackItems.isEmpty());
    boolean backoff = lastScrobbleTime + nextScrobbleDelay > System.currentTimeMillis();
    if (!isConnected || !client.isAuthenticated() || backoff) {
        return;
    }
    trackLover.lovePending();
    if (isScrobbling || !tracksPending) {
        return;
    }
    List<PlaybackItem> playbackItems = new ArrayList<>(pendingPlaybackItems);
    pendingPlaybackItems.clear();
    scroballDB.clearPendingPlaybackItems();
    if (!playbackItems.isEmpty()) {
        Log.d(TAG, "Re-processing queued items with missing durations.");
    }
    for (PlaybackItem playbackItem : playbackItems) {
        fetchTrackDurationAndSubmit(playbackItem);
    }
    if (pending.isEmpty()) {
        return;
    }
    isScrobbling = true;
    final List<Scrobble> tracksToScrobble = new ArrayList<>(pending);
    while (tracksToScrobble.size() > MAX_SCROBBLES) {
        tracksToScrobble.remove(tracksToScrobble.size() - 1);
    }
    client.scrobbleTracks(tracksToScrobble, message -> {
        List<LastfmClient.Result> results = (List<LastfmClient.Result>) message.obj;
        boolean shouldBackoff = false;
        for (int i = 0; i < results.size(); i++) {
            LastfmClient.Result result = results.get(i);
            Scrobble scrobble = tracksToScrobble.get(i);
            if (result.isSuccessful()) {
                scrobble.status().setScrobbled(true);
                scroballDB.writeScrobble(scrobble);
                pending.remove(scrobble);
            } else {
                int errorCode = result.errorCode();
                if (!LastfmClient.isTransientError(errorCode)) {
                    pending.remove(scrobble);
                    shouldBackoff = true;
                }
                if (LastfmClient.isAuthenticationError(errorCode)) {
                    notificationManager.notifyAuthError();
                    ScroballApplication.getEventBus().post(AuthErrorEvent.create(errorCode));
                }
                scrobble.status().setErrorCode(errorCode);
                scroballDB.writeScrobble(scrobble);
            }
        }
        isScrobbling = false;
        lastScrobbleTime = System.currentTimeMillis();
        if (shouldBackoff) {
            // Back off starting at 1 second, up to an hour max.
            if (nextScrobbleDelay == 0) {
                nextScrobbleDelay = 1000;
            } else if (nextScrobbleDelay < 60 * 60 * 1000) {
                nextScrobbleDelay *= 4;
            }
        } else {
            nextScrobbleDelay = 0;
            // There may be more tracks waiting to scrobble. Keep going.
            scrobblePending();
        }
        return false;
    });
}
Also used : NetworkInfo(android.net.NetworkInfo) ArrayList(java.util.ArrayList) Result(com.softartdev.lastfm.Result) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

NetworkInfo (android.net.NetworkInfo)3 Result (com.softartdev.lastfm.Result)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1