diff options
-rw-r--r-- | res/values/strings.xml | 2 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/service/download/DownloadService.java | 47 |
2 files changed, 28 insertions, 21 deletions
diff --git a/res/values/strings.xml b/res/values/strings.xml index 9586fa5d6..425152192 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -87,7 +87,7 @@ <string name="pref_pauseOnHeadsetDisconnect_title">Headphones disconnect</string> <string name="pref_mobileUpdate_title">Mobile updates</string> <string name="pref_mobileUpdate_sum">Allow updates over the mobile data connection</string> - <string name="download_report_title">All downloads completed</string> + <string name="download_report_title">Downloads completed</string> <string name="refresh_label">Refresh</string> <string name="external_storage_error_msg">No external storage is available. Please make sure that external storage is mounted so that the app can work properly.</string> <string name="share_link_label">Share link</string> diff --git a/src/de/danoeh/antennapod/service/download/DownloadService.java b/src/de/danoeh/antennapod/service/download/DownloadService.java index 42ef635e6..6a8a3cb2f 100644 --- a/src/de/danoeh/antennapod/service/download/DownloadService.java +++ b/src/de/danoeh/antennapod/service/download/DownloadService.java @@ -11,6 +11,7 @@ import java.lang.Thread.UncaughtExceptionHandler; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; @@ -70,6 +71,9 @@ public class DownloadService extends Service { public static final String ACTION_CANCEL_DOWNLOAD = "action.de.danoeh.antennapod.service.cancelDownload"; public static final String ACTION_CANCEL_ALL_DOWNLOADS = "action.de.danoeh.antennapod.service.cancelAllDownloads"; + /** Is used for sending the delete intent for the report notification */ + private static final String ACTION_REPORT_DELETED = "action.de.danoeh.antennapod.service.reportDeleted"; + /** Extra for ACTION_CANCEL_DOWNLOAD */ public static final String EXTRA_DOWNLOAD_URL = "downloadUrl"; @@ -91,7 +95,7 @@ public class DownloadService extends Service { public static final int DOWNLOAD_TYPE_MEDIA = 2; public static final int DOWNLOAD_TYPE_IMAGE = 3; - private ArrayList<DownloadStatus> completedDownloads; + private CopyOnWriteArrayList<DownloadStatus> completedDownloads; private ExecutorService syncExecutor; private ExecutorService downloadExecutor; @@ -135,7 +139,8 @@ public class DownloadService extends Service { Log.d(TAG, "Service started"); isRunning = true; handler = new Handler(); - completedDownloads = new ArrayList<DownloadStatus>(); + completedDownloads = new CopyOnWriteArrayList<DownloadStatus>( + new ArrayList<DownloadStatus>()); downloads = new ArrayList<Downloader>(); registerReceiver(downloadQueued, new IntentFilter( ACTION_ENQUEUE_DOWNLOAD)); @@ -144,6 +149,7 @@ public class DownloadService extends Service { cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_ALL_DOWNLOADS); cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_DOWNLOAD); registerReceiver(cancelDownloadReceiver, cancelDownloadReceiverFilter); + registerReceiver(reportDeleted, new IntentFilter(ACTION_REPORT_DELETED)); syncExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override @@ -191,7 +197,6 @@ public class DownloadService extends Service { mediaplayer.release(); unregisterReceiver(cancelDownloadReceiver); unregisterReceiver(downloadQueued); - createReport(); } private void setupNotification() { @@ -414,29 +419,25 @@ public class DownloadService extends Service { sendBroadcast(intent); } + private BroadcastReceiver reportDeleted = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(ACTION_REPORT_DELETED)) { + completedDownloads.clear(); + } + } + }; + /** * Creates a notification at the end of the service lifecycle to notify the * user about the number of completed downloads. A report will only be * created if the number of feeds is > 1 or if at least one media file was * downloaded. */ - private void createReport() { + private void updateReport() { // check if report should be created - boolean createReport = false; - int feedCount = 0; - for (DownloadStatus status : completedDownloads) { - if (status.getFeedFile().getClass() == Feed.class) { - feedCount++; - if (feedCount > 1) { - createReport = true; - break; - } - } else if (status.getFeedFile().getClass() == FeedMedia.class) { - createReport = true; - break; - } - } - if (createReport) { + if (!completedDownloads.isEmpty()) { if (AppConfig.DEBUG) Log.d(TAG, "Creating report"); int successfulDownloads = 0; @@ -464,7 +465,12 @@ public class DownloadService extends Service { .setContentIntent( PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0)) - .setAutoCancel(true).getNotification(); + .setAutoCancel(true) + .setDeleteIntent( + PendingIntent.getBroadcast(this, 0, new Intent( + ACTION_REPORT_DELETED), + PendingIntent.FLAG_UPDATE_CURRENT)) + .getNotification(); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(REPORT_ID, notification); @@ -494,6 +500,7 @@ public class DownloadService extends Service { NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID, notificationBuilder.getNotification()); } + updateReport(); } /** Is called whenever a Feed is downloaded */ |