Search in sources :

Example 1 with GpodnetUploadChangesResponse

use of de.danoeh.antennapod.core.gpoddernet.model.GpodnetUploadChangesResponse in project AntennaPod by AntennaPod.

the class GpodnetSyncService method syncSubscriptionChanges.

private synchronized void syncSubscriptionChanges() {
    final long timestamp = GpodnetPreferences.getLastSubscriptionSyncTimestamp();
    try {
        final List<String> localSubscriptions = DBReader.getFeedListDownloadUrls();
        Collection<String> localAdded = GpodnetPreferences.getAddedFeedsCopy();
        Collection<String> localRemoved = GpodnetPreferences.getRemovedFeedsCopy();
        GpodnetService service = tryLogin();
        // first sync: download all subscriptions...
        GpodnetSubscriptionChange subscriptionChanges = service.getSubscriptionChanges(GpodnetPreferences.getUsername(), GpodnetPreferences.getDeviceID(), timestamp);
        long newTimeStamp = subscriptionChanges.getTimestamp();
        Log.d(TAG, "Downloaded subscription changes: " + subscriptionChanges);
        processSubscriptionChanges(localSubscriptions, localAdded, localRemoved, subscriptionChanges);
        if (timestamp == 0) {
            // this is this apps first sync with gpodder:
            // only submit changes gpodder has not just sent us
            localAdded = localSubscriptions;
            localAdded.removeAll(subscriptionChanges.getAdded());
            localRemoved.removeAll(subscriptionChanges.getRemoved());
        }
        if (localAdded.size() > 0 || localRemoved.size() > 0) {
            Log.d(TAG, String.format("Uploading subscriptions, Added: %s\nRemoved: %s", localAdded, localRemoved));
            GpodnetUploadChangesResponse uploadResponse = service.uploadChanges(GpodnetPreferences.getUsername(), GpodnetPreferences.getDeviceID(), localAdded, localRemoved);
            newTimeStamp = uploadResponse.timestamp;
            Log.d(TAG, "Upload changes response: " + uploadResponse);
            GpodnetPreferences.removeAddedFeeds(localAdded);
            GpodnetPreferences.removeRemovedFeeds(localRemoved);
        }
        GpodnetPreferences.setLastSubscriptionSyncTimestamp(newTimeStamp);
        GpodnetPreferences.setLastSyncAttempt(true, System.currentTimeMillis());
        clearErrorNotifications();
    } catch (GpodnetServiceException e) {
        e.printStackTrace();
        updateErrorNotification(e);
    } catch (DownloadRequestException e) {
        e.printStackTrace();
    }
}
Also used : GpodnetSubscriptionChange(de.danoeh.antennapod.core.gpoddernet.model.GpodnetSubscriptionChange) GpodnetServiceException(de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException) GpodnetService(de.danoeh.antennapod.core.gpoddernet.GpodnetService) GpodnetUploadChangesResponse(de.danoeh.antennapod.core.gpoddernet.model.GpodnetUploadChangesResponse) DownloadRequestException(de.danoeh.antennapod.core.storage.DownloadRequestException)

Aggregations

GpodnetService (de.danoeh.antennapod.core.gpoddernet.GpodnetService)1 GpodnetServiceException (de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException)1 GpodnetSubscriptionChange (de.danoeh.antennapod.core.gpoddernet.model.GpodnetSubscriptionChange)1 GpodnetUploadChangesResponse (de.danoeh.antennapod.core.gpoddernet.model.GpodnetUploadChangesResponse)1 DownloadRequestException (de.danoeh.antennapod.core.storage.DownloadRequestException)1