From ef121892f7705e97bb0060b243a72258fe7cd887 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 27 Mar 2020 23:47:18 +0100 Subject: Migrated to WorkManager to allow retrying --- core/src/main/AndroidManifest.xml | 5 -- .../danoeh/antennapod/core/sync/SyncService.java | 95 ++++++++++++++-------- 2 files changed, 61 insertions(+), 39 deletions(-) (limited to 'core/src') diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index 3527ff010..1f6c36c40 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -25,11 +25,6 @@ - getQueuedEpisodeActions() { ArrayList actions = new ArrayList<>(); try { - SharedPreferences prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + SharedPreferences prefs = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); String json = prefs.getString(PREF_QUEUED_EPISODE_ACTIONS, "[]"); JSONArray queue = new JSONArray(json); for (int i = 0; i < queue.length(); i++) { @@ -184,7 +205,7 @@ public class SyncService extends SafeJobIntentService { private List getQueuedRemovedFeeds() { ArrayList actions = new ArrayList<>(); try { - SharedPreferences prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + SharedPreferences prefs = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); String json = prefs.getString(PREF_QUEUED_FEEDS_REMOVED, "[]"); JSONArray queue = new JSONArray(json); for (int i = 0; i < queue.length(); i++) { @@ -199,7 +220,7 @@ public class SyncService extends SafeJobIntentService { private List getQueuedAddedFeeds() { ArrayList actions = new ArrayList<>(); try { - SharedPreferences prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + SharedPreferences prefs = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); String json = prefs.getString(PREF_QUEUED_FEEDS_ADDED, "[]"); JSONArray queue = new JSONArray(json); for (int i = 0; i < queue.length(); i++) { @@ -212,7 +233,7 @@ public class SyncService extends SafeJobIntentService { } private void syncSubscriptions() throws SyncServiceException { - final long lastSync = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) + final long lastSync = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) .getLong(PREF_LAST_SUBSCRIPTION_SYNC_TIMESTAMP, 0); final List localSubscriptions = DBReader.getFeedListDownloadUrls(); SubscriptionChanges subscriptionChanges = syncServiceImpl.getSubscriptionChanges(lastSync); @@ -226,7 +247,7 @@ public class SyncService extends SafeJobIntentService { if (!URLChecker.containsUrl(localSubscriptions, downloadUrl) && !queuedRemovedFeeds.contains(downloadUrl)) { Feed feed = new Feed(downloadUrl, null); try { - DownloadRequester.getInstance().downloadFeed(this, feed); + DownloadRequester.getInstance().downloadFeed(getApplicationContext(), feed); } catch (DownloadRequestException e) { e.printStackTrace(); } @@ -236,7 +257,7 @@ public class SyncService extends SafeJobIntentService { // remove subscription if not just subscribed (again) for (String downloadUrl : subscriptionChanges.getRemoved()) { if (!queuedAddedFeeds.contains(downloadUrl)) { - DBTasks.removeFeedWithDownloadUrl(this, downloadUrl); + DBTasks.removeFeedWithDownloadUrl(getApplicationContext(), downloadUrl); } } @@ -254,19 +275,19 @@ public class SyncService extends SafeJobIntentService { synchronized (lock) { UploadChangesResponse uploadResponse = syncServiceImpl .uploadSubscriptionChanges(queuedAddedFeeds, queuedRemovedFeeds); - getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() + getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() .putString(PREF_QUEUED_FEEDS_ADDED, "[]").apply(); - getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() + getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() .putString(PREF_QUEUED_FEEDS_REMOVED, "[]").apply(); newTimeStamp = uploadResponse.timestamp; } } - getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() + getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() .putLong(PREF_LAST_SUBSCRIPTION_SYNC_TIMESTAMP, newTimeStamp).apply(); } private void syncEpisodeActions() throws SyncServiceException { - final long lastSync = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) + final long lastSync = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) .getLong(PREF_LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, 0); EpisodeActionChanges getResponse = syncServiceImpl.getEpisodeActionChanges(lastSync); long newTimeStamp = getResponse.getTimestamp(); @@ -281,11 +302,11 @@ public class SyncService extends SafeJobIntentService { UploadChangesResponse postResponse = syncServiceImpl.uploadEpisodeActions(queuedEpisodeActions); newTimeStamp = postResponse.timestamp; Log.d(TAG, "Upload episode response: " + postResponse); - getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() + getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() .putString(PREF_QUEUED_EPISODE_ACTIONS, "[]").apply(); } } - getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() + getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit() .putLong(PREF_LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, newTimeStamp).apply(); } @@ -356,26 +377,32 @@ public class SyncService extends SafeJobIntentService { } private void clearErrorNotifications() { - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager nm = (NotificationManager) getApplicationContext() + .getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel(R.id.notification_gpodnet_sync_error); nm.cancel(R.id.notification_gpodnet_sync_autherror); } private void updateErrorNotification(SyncServiceException exception) { Log.d(TAG, "Posting error notification"); - final String description = getString(R.string.gpodnetsync_error_descr) + exception.getMessage(); - - Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); - Notification notification = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_ERROR) - .setContentTitle(getString(R.string.gpodnetsync_error_title)) + final String description = getApplicationContext().getString(R.string.gpodnetsync_error_descr) + + exception.getMessage(); + + Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage( + getApplicationContext().getPackageName()); + PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, + PendingIntent.FLAG_UPDATE_CURRENT); + Notification notification = new NotificationCompat.Builder(getApplicationContext(), + NotificationUtils.CHANNEL_ID_ERROR) + .setContentTitle(getApplicationContext().getString(R.string.gpodnetsync_error_title)) .setContentText(description) .setContentIntent(pendingIntent) .setSmallIcon(R.drawable.ic_notification_sync_error) .setAutoCancel(true) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .build(); - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager nm = (NotificationManager) getApplicationContext() + .getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(R.id.notification_gpodnet_sync_error, notification); } } -- cgit v1.2.3