Search in sources :

Example 1 with EpisodeActionChanges

use of de.danoeh.antennapod.net.sync.model.EpisodeActionChanges in project AntennaPod by AntennaPod.

the class ResponseMapper method readEpisodeActionsFromJsonObject.

public static EpisodeActionChanges readEpisodeActionsFromJsonObject(@NonNull JSONObject object) throws JSONException {
    List<EpisodeAction> episodeActions = new ArrayList<>();
    long timestamp = object.getLong("timestamp");
    JSONArray jsonActions = object.getJSONArray("actions");
    for (int i = 0; i < jsonActions.length(); i++) {
        JSONObject jsonAction = jsonActions.getJSONObject(i);
        EpisodeAction episodeAction = EpisodeAction.readFromJsonObject(jsonAction);
        if (episodeAction != null) {
            episodeActions.add(episodeAction);
        }
    }
    return new EpisodeActionChanges(episodeActions, timestamp);
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) EpisodeAction(de.danoeh.antennapod.net.sync.model.EpisodeAction) EpisodeActionChanges(de.danoeh.antennapod.net.sync.model.EpisodeActionChanges)

Example 2 with EpisodeActionChanges

use of de.danoeh.antennapod.net.sync.model.EpisodeActionChanges in project AntennaPod by AntennaPod.

the class SyncService method syncEpisodeActions.

private void syncEpisodeActions(ISyncService syncServiceImpl) throws SyncServiceException {
    final long lastSync = SynchronizationSettings.getLastEpisodeActionSynchronizationTimestamp();
    EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_episodes_download));
    EpisodeActionChanges getResponse = syncServiceImpl.getEpisodeActionChanges(lastSync);
    long newTimeStamp = getResponse.getTimestamp();
    List<EpisodeAction> remoteActions = getResponse.getEpisodeActions();
    processEpisodeActions(remoteActions);
    // upload local actions
    EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_episodes_upload));
    List<EpisodeAction> queuedEpisodeActions = synchronizationQueueStorage.getQueuedEpisodeActions();
    if (lastSync == 0) {
        EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_upload_played));
        List<FeedItem> readItems = DBReader.getPlayedItems();
        Log.d(TAG, "First sync. Upload state for all " + readItems.size() + " played episodes");
        for (FeedItem item : readItems) {
            FeedMedia media = item.getMedia();
            if (media == null) {
                continue;
            }
            EpisodeAction played = new EpisodeAction.Builder(item, EpisodeAction.PLAY).currentTimestamp().started(media.getDuration() / 1000).position(media.getDuration() / 1000).total(media.getDuration() / 1000).build();
            queuedEpisodeActions.add(played);
        }
    }
    if (queuedEpisodeActions.size() > 0) {
        LockingAsyncExecutor.lock.lock();
        try {
            Log.d(TAG, "Uploading " + queuedEpisodeActions.size() + " actions: " + StringUtils.join(queuedEpisodeActions, ", "));
            UploadChangesResponse postResponse = syncServiceImpl.uploadEpisodeActions(queuedEpisodeActions);
            newTimeStamp = postResponse.timestamp;
            Log.d(TAG, "Upload episode response: " + postResponse);
            synchronizationQueueStorage.clearEpisodeActionQueue();
        } finally {
            LockingAsyncExecutor.lock.unlock();
        }
    }
    SynchronizationSettings.setLastEpisodeActionSynchronizationAttemptTimestamp(newTimeStamp);
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) SyncServiceEvent(de.danoeh.antennapod.event.SyncServiceEvent) UploadChangesResponse(de.danoeh.antennapod.net.sync.model.UploadChangesResponse) EpisodeActionChanges(de.danoeh.antennapod.net.sync.model.EpisodeActionChanges) EpisodeAction(de.danoeh.antennapod.net.sync.model.EpisodeAction)

Aggregations

EpisodeAction (de.danoeh.antennapod.net.sync.model.EpisodeAction)2 EpisodeActionChanges (de.danoeh.antennapod.net.sync.model.EpisodeActionChanges)2 SyncServiceEvent (de.danoeh.antennapod.event.SyncServiceEvent)1 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)1 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)1 UploadChangesResponse (de.danoeh.antennapod.net.sync.model.UploadChangesResponse)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1