Search in sources :

Example 1 with SubscriptionChanges

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

the class ResponseMapper method readSubscriptionChangesFromJsonObject.

public static SubscriptionChanges 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 SubscriptionChanges(added, removed, timestamp);
}
Also used : JSONArray(org.json.JSONArray) SubscriptionChanges(de.danoeh.antennapod.net.sync.model.SubscriptionChanges) LinkedList(java.util.LinkedList)

Example 2 with SubscriptionChanges

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

SubscriptionChanges (de.danoeh.antennapod.net.sync.model.SubscriptionChanges)2 DownloadRequest (de.danoeh.antennapod.core.service.download.DownloadRequest)1 SyncServiceEvent (de.danoeh.antennapod.event.SyncServiceEvent)1 Feed (de.danoeh.antennapod.model.feed.Feed)1 UploadChangesResponse (de.danoeh.antennapod.net.sync.model.UploadChangesResponse)1 LinkedList (java.util.LinkedList)1 JSONArray (org.json.JSONArray)1