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();
}
}
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);
}
Aggregations