Search in sources :

Example 1 with GpodnetSubscriptionChange

use of de.danoeh.antennapod.core.gpoddernet.model.GpodnetSubscriptionChange 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)

Example 2 with GpodnetSubscriptionChange

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

the class GpodnetService method readSubscriptionChangesFromJSONObject.

private GpodnetSubscriptionChange readSubscriptionChangesFromJSONObject(@NonNull JSONObject object) throws JSONException {
    List<String> added = new LinkedList<>();
    JSONArray jsonAdded = object.getJSONArray("add");
    for (int i = 0; i < jsonAdded.length(); i++) {
        String addedUrl = jsonAdded.getString(i);
        // gpodder escapes colons unnecessarily
        addedUrl = addedUrl.replace("%3A", ":");
        added.add(addedUrl);
    }
    List<String> removed = new LinkedList<>();
    JSONArray jsonRemoved = object.getJSONArray("remove");
    for (int i = 0; i < jsonRemoved.length(); i++) {
        String removedUrl = jsonRemoved.getString(i);
        // gpodder escapes colons unnecessarily
        removedUrl = removedUrl.replace("%3A", ":");
        removed.add(removedUrl);
    }
    long timestamp = object.getLong("timestamp");
    return new GpodnetSubscriptionChange(added, removed, timestamp);
}
Also used : GpodnetSubscriptionChange(de.danoeh.antennapod.core.gpoddernet.model.GpodnetSubscriptionChange) JSONArray(org.json.JSONArray) LinkedList(java.util.LinkedList)

Aggregations

GpodnetSubscriptionChange (de.danoeh.antennapod.core.gpoddernet.model.GpodnetSubscriptionChange)2 GpodnetService (de.danoeh.antennapod.core.gpoddernet.GpodnetService)1 GpodnetServiceException (de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException)1 GpodnetUploadChangesResponse (de.danoeh.antennapod.core.gpoddernet.model.GpodnetUploadChangesResponse)1 DownloadRequestException (de.danoeh.antennapod.core.storage.DownloadRequestException)1 LinkedList (java.util.LinkedList)1 JSONArray (org.json.JSONArray)1