@@ -132,14 +132,34 @@ public static class MultipleDownloadsCompletedReceiver extends BroadcastReceiver
132132 */
133133 @ Override
134134 public void onReceive (Context context , Intent intent ) {
135- Log .i (LOG_TAG , "Received Notification:" );
136135 if (intent .getAction ().equalsIgnoreCase (DownloadManager .ACTION_DOWNLOAD_COMPLETE )) {
137136 synchronized (this ) {
138- ++mNumDownloadsCompleted ;
139- Log .i (LOG_TAG , "MultipleDownloadsCompletedReceiver got intent: " +
140- intent .getAction () + " --> total count: " + mNumDownloadsCompleted );
141- Bundle extras = intent .getExtras ();
142- downloadIds .add (new Long (extras .getLong (DownloadManager .EXTRA_DOWNLOAD_ID )));
137+ long id = intent .getExtras ().getLong (DownloadManager .EXTRA_DOWNLOAD_ID );
138+ Log .i (LOG_TAG , "Received Notification for download: " + id );
139+ if (!downloadIds .contains (id )) {
140+ ++mNumDownloadsCompleted ;
141+ Log .i (LOG_TAG , "MultipleDownloadsCompletedReceiver got intent: " +
142+ intent .getAction () + " --> total count: " + mNumDownloadsCompleted );
143+ downloadIds .add (id );
144+
145+ DownloadManager dm = (DownloadManager )context .getSystemService (
146+ Context .DOWNLOAD_SERVICE );
147+
148+ Cursor cursor = dm .query (new Query ().setFilterById (id ));
149+ try {
150+ if (cursor .moveToFirst ()) {
151+ int status = cursor .getInt (cursor .getColumnIndex (
152+ DownloadManager .COLUMN_STATUS ));
153+ Log .i (LOG_TAG , "Download status is: " + status );
154+ } else {
155+ fail ("No status found for completed download!" );
156+ }
157+ } finally {
158+ cursor .close ();
159+ }
160+ } else {
161+ Log .i (LOG_TAG , "Notification for id: " + id + " has already been made." );
162+ }
143163 }
144164 }
145165 }
@@ -621,28 +641,48 @@ protected File createFileOnSD(String filename, long fileSize, DataType type,
621641 /**
622642 * Helper to wait for a particular download to finish, or else a timeout to occur
623643 *
644+ * Does not wait for a receiver notification of the download.
645+ *
624646 * @param id The download id to query on (wait for)
625647 */
626- protected void waitForDownloadOrTimeout (long id ) throws TimeoutException ,
648+ protected void waitForDownloadOrTimeout_skipNotification (long id ) throws TimeoutException ,
627649 InterruptedException {
628650 waitForDownloadOrTimeout (id , WAIT_FOR_DOWNLOAD_POLL_TIME , MAX_WAIT_FOR_DOWNLOAD_TIME );
629651 }
630652
631653 /**
632654 * Helper to wait for a particular download to finish, or else a timeout to occur
633655 *
656+ * Also guarantees a notification has been posted for the download.
657+ *
658+ * @param id The download id to query on (wait for)
659+ */
660+ protected void waitForDownloadOrTimeout (long id ) throws TimeoutException ,
661+ InterruptedException {
662+ waitForDownloadOrTimeout_skipNotification (id );
663+ waitForReceiverNotifications (1 );
664+ }
665+
666+ /**
667+ * Helper to wait for a particular download to finish, or else a timeout to occur
668+ *
669+ * Also guarantees a notification has been posted for the download.
670+ *
634671 * @param id The download id to query on (wait for)
635672 * @param poll The amount of time to wait
636673 * @param timeoutMillis The max time (in ms) to wait for the download(s) to complete
637674 */
638675 protected void waitForDownloadOrTimeout (long id , long poll , long timeoutMillis )
639676 throws TimeoutException , InterruptedException {
640677 doWaitForDownloadsOrTimeout (new Query ().setFilterById (id ), poll , timeoutMillis );
678+ waitForReceiverNotifications (1 );
641679 }
642680
643681 /**
644682 * Helper to wait for all downloads to finish, or else a specified timeout to occur
645683 *
684+ * Makes no guaranee that notifications have been posted for all downloads.
685+ *
646686 * @param poll The amount of time to wait
647687 * @param timeoutMillis The max time (in ms) to wait for the download(s) to complete
648688 */
@@ -654,6 +694,8 @@ protected void waitForDownloadsOrTimeout(long poll, long timeoutMillis) throws T
654694 /**
655695 * Helper to wait for all downloads to finish, or else a timeout to occur, but does not throw
656696 *
697+ * Also guarantees a notification has been posted for the download.
698+ *
657699 * @param id The id of the download to query against
658700 * @param poll The amount of time to wait
659701 * @param timeoutMillis The max time (in ms) to wait for the download(s) to complete
@@ -662,6 +704,7 @@ protected void waitForDownloadsOrTimeout(long poll, long timeoutMillis) throws T
662704 protected boolean waitForDownloadOrTimeoutNoThrow (long id , long poll , long timeoutMillis ) {
663705 try {
664706 doWaitForDownloadsOrTimeout (new Query ().setFilterById (id ), poll , timeoutMillis );
707+ waitForReceiverNotifications (1 );
665708 } catch (TimeoutException e ) {
666709 return false ;
667710 }
@@ -717,9 +760,8 @@ protected void doWaitForDownloadsOrTimeout(Query query, long poll, long timeoutM
717760 Cursor cursor = mDownloadManager .query (query );
718761
719762 try {
720- // @TODO: there may be a little cleaner way to check for success, perhaps
721- // via STATUS_SUCCESSFUL and/or STATUS_FAILED
722- if (cursor .getCount () == 0 && mReceiver .numDownloadsCompleted () > 0 ) {
763+ if (cursor .getCount () == 0 ) {
764+ Log .i (LOG_TAG , "All downloads should be done..." );
723765 break ;
724766 }
725767 currentWaitTime = timeoutWait (currentWaitTime , poll , timeoutMillis ,
@@ -778,6 +820,36 @@ protected void waitForDownloadToStart(long dlRequest) throws Exception {
778820 }
779821 }
780822
823+ /**
824+ * Convenience function to wait for just 1 notification of a download.
825+ *
826+ * @throws Exception if timed out while waiting
827+ */
828+ protected void waitForReceiverNotification () throws Exception {
829+ waitForReceiverNotifications (1 );
830+ }
831+
832+ /**
833+ * Synchronously waits for our receiver to receive notification for a given number of
834+ * downloads.
835+ *
836+ * @param targetNumber The number of notifications for unique downloads to wait for; pass in
837+ * -1 to not wait for notification.
838+ * @throws Exception if timed out while waiting
839+ */
840+ protected void waitForReceiverNotifications (int targetNumber ) throws TimeoutException {
841+ int count = mReceiver .numDownloadsCompleted ();
842+ int currentWaitTime = 0 ;
843+
844+ while (count < targetNumber ) {
845+ Log .i (LOG_TAG , "Waiting for notification of downloads..." );
846+ currentWaitTime = timeoutWait (currentWaitTime , WAIT_FOR_DOWNLOAD_POLL_TIME ,
847+ MAX_WAIT_FOR_DOWNLOAD_TIME , "Timed out waiting for download notifications!"
848+ + " Received " + count + "notifications." );
849+ count = mReceiver .numDownloadsCompleted ();
850+ }
851+ }
852+
781853 /**
782854 * Synchronously waits for a file to increase in size (such as to monitor that a download is
783855 * progressing).
0 commit comments