Search in sources :

Example 1 with UploadChangesResponse

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

the class GpodnetService method uploadEpisodeActions.

/**
 * Updates the episode actions
 * <p/>
 * This method requires authentication.
 *
 * @param episodeActions Collection of episode actions.
 * @return a GpodnetUploadChangesResponse. See {@link GpodnetUploadChangesResponse}
 * for details.
 * @throws GpodnetServiceException            if added or removed contain duplicates or if there
 *                                            is an authentication error.
 */
@Override
public UploadChangesResponse uploadEpisodeActions(List<EpisodeAction> episodeActions) throws SyncServiceException {
    requireLoggedIn();
    UploadChangesResponse response = null;
    for (int i = 0; i < episodeActions.size(); i += UPLOAD_BULK_SIZE) {
        response = uploadEpisodeActionsPartial(episodeActions, i, Math.min(episodeActions.size(), i + UPLOAD_BULK_SIZE));
    }
    return response;
}
Also used : UploadChangesResponse(de.danoeh.antennapod.net.sync.model.UploadChangesResponse) GpodnetUploadChangesResponse(de.danoeh.antennapod.net.sync.gpoddernet.model.GpodnetUploadChangesResponse)

Example 2 with UploadChangesResponse

use of de.danoeh.antennapod.net.sync.model.UploadChangesResponse 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)

Example 3 with UploadChangesResponse

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

the class SyncService method syncSubscriptions.

private void syncSubscriptions(ISyncService syncServiceImpl) throws SyncServiceException {
    final long lastSync = SynchronizationSettings.getLastSubscriptionSynchronizationTimestamp();
    EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_subscriptions));
    final List<String> localSubscriptions = DBReader.getFeedListDownloadUrls();
    SubscriptionChanges subscriptionChanges = syncServiceImpl.getSubscriptionChanges(lastSync);
    long newTimeStamp = subscriptionChanges.getTimestamp();
    List<String> queuedRemovedFeeds = synchronizationQueueStorage.getQueuedRemovedFeeds();
    List<String> queuedAddedFeeds = synchronizationQueueStorage.getQueuedAddedFeeds();
    Log.d(TAG, "Downloaded subscription changes: " + subscriptionChanges);
    for (String downloadUrl : subscriptionChanges.getAdded()) {
        if (!URLChecker.containsUrl(localSubscriptions, downloadUrl) && !queuedRemovedFeeds.contains(downloadUrl)) {
            Feed feed = new Feed(downloadUrl, null);
            DownloadRequest.Builder builder = DownloadRequestCreator.create(feed);
            DownloadService.download(getApplicationContext(), false, builder.build());
        }
    }
    // remove subscription if not just subscribed (again)
    for (String downloadUrl : subscriptionChanges.getRemoved()) {
        if (!queuedAddedFeeds.contains(downloadUrl)) {
            DBTasks.removeFeedWithDownloadUrl(getApplicationContext(), downloadUrl);
        }
    }
    if (lastSync == 0) {
        Log.d(TAG, "First sync. Adding all local subscriptions.");
        queuedAddedFeeds = localSubscriptions;
        queuedAddedFeeds.removeAll(subscriptionChanges.getAdded());
        queuedRemovedFeeds.removeAll(subscriptionChanges.getRemoved());
    }
    if (queuedAddedFeeds.size() > 0 || queuedRemovedFeeds.size() > 0) {
        Log.d(TAG, "Added: " + StringUtils.join(queuedAddedFeeds, ", "));
        Log.d(TAG, "Removed: " + StringUtils.join(queuedRemovedFeeds, ", "));
        LockingAsyncExecutor.lock.lock();
        try {
            UploadChangesResponse uploadResponse = syncServiceImpl.uploadSubscriptionChanges(queuedAddedFeeds, queuedRemovedFeeds);
            synchronizationQueueStorage.clearFeedQueues();
            newTimeStamp = uploadResponse.timestamp;
        } finally {
            LockingAsyncExecutor.lock.unlock();
        }
    }
    SynchronizationSettings.setLastSubscriptionSynchronizationAttemptTimestamp(newTimeStamp);
}
Also used : DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) SubscriptionChanges(de.danoeh.antennapod.net.sync.model.SubscriptionChanges) SyncServiceEvent(de.danoeh.antennapod.event.SyncServiceEvent) UploadChangesResponse(de.danoeh.antennapod.net.sync.model.UploadChangesResponse) Feed(de.danoeh.antennapod.model.feed.Feed)

Aggregations

UploadChangesResponse (de.danoeh.antennapod.net.sync.model.UploadChangesResponse)3 SyncServiceEvent (de.danoeh.antennapod.event.SyncServiceEvent)2 DownloadRequest (de.danoeh.antennapod.core.service.download.DownloadRequest)1 Feed (de.danoeh.antennapod.model.feed.Feed)1 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)1 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)1 GpodnetUploadChangesResponse (de.danoeh.antennapod.net.sync.gpoddernet.model.GpodnetUploadChangesResponse)1 EpisodeAction (de.danoeh.antennapod.net.sync.model.EpisodeAction)1 EpisodeActionChanges (de.danoeh.antennapod.net.sync.model.EpisodeActionChanges)1 SubscriptionChanges (de.danoeh.antennapod.net.sync.model.SubscriptionChanges)1