/**
   * 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 successfully downloaded
   * feeds is bigger than 1 or if there is at least one failed download which is not an image or if
   * there is at least one downloaded media file.
   */
  private void updateReport() {
    // check if report should be created
    boolean createReport = false;
    int successfulDownloads = 0;
    int failedDownloads = 0;

    // a download report is created if at least one download has failed
    // (excluding failed image downloads)
    for (DownloadStatus status : completedDownloads) {
      if (status.isSuccessful()) {
        successfulDownloads++;
      } else if (!status.isCancelled()) {
        if (status.getFeedfileType() != FeedImage.FEEDFILETYPE_FEEDIMAGE) {
          createReport = true;
        }
        failedDownloads++;
      }
    }

    if (createReport) {
      if (BuildConfig.DEBUG) Log.d(TAG, "Creating report");
      Intent intent = new Intent(this, MainActivity.class);
      intent.putExtra(MainActivity.EXTRA_NAV_TYPE, NavListAdapter.VIEW_TYPE_NAV);
      intent.putExtra(MainActivity.EXTRA_NAV_INDEX, MainActivity.POS_DOWNLOADS);
      Bundle args = new Bundle();
      args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG);
      intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args);

      // create notification object
      Notification notification =
          new NotificationCompat.Builder(this)
              .setTicker(getString(de.danoeh.antennapod.R.string.download_report_title))
              .setContentTitle(getString(de.danoeh.antennapod.R.string.download_report_title))
              .setContentText(
                  String.format(
                      getString(R.string.download_report_content),
                      successfulDownloads,
                      failedDownloads))
              .setSmallIcon(R.drawable.stat_notify_sync)
              .setLargeIcon(
                  BitmapFactory.decodeResource(getResources(), R.drawable.stat_notify_sync))
              .setContentIntent(
                  PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
              .setAutoCancel(true)
              .build();
      NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      nm.notify(REPORT_ID, notification);
    } else {
      if (BuildConfig.DEBUG) Log.d(TAG, "No report is created");
    }
    completedDownloads.clear();
  }
        @Override
        public void run() {
          if (BuildConfig.DEBUG) Log.d(TAG, "downloadCompletionThread was started");
          while (!isInterrupted()) {
            try {
              Downloader downloader = downloadExecutor.take().get();
              if (BuildConfig.DEBUG) Log.d(TAG, "Received 'Download Complete' - message.");
              removeDownload(downloader);
              DownloadStatus status = downloader.getResult();
              boolean successful = status.isSuccessful();

              final int type = status.getFeedfileType();
              if (successful) {
                if (type == Feed.FEEDFILETYPE_FEED) {
                  handleCompletedFeedDownload(downloader.getDownloadRequest());
                } else if (type == FeedImage.FEEDFILETYPE_FEEDIMAGE) {
                  handleCompletedImageDownload(status, downloader.getDownloadRequest());
                } else if (type == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
                  handleCompletedFeedMediaDownload(status, downloader.getDownloadRequest());
                }
              } else {
                numberOfDownloads.decrementAndGet();
                if (!status.isCancelled()) {
                  if (status.getReason() == DownloadError.ERROR_UNAUTHORIZED) {
                    postAuthenticationNotification(downloader.getDownloadRequest());
                  } else if (status.getReason() == DownloadError.ERROR_HTTP_DATA_ERROR
                      && Integer.valueOf(status.getReasonDetailed())
                          == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {

                    Log.d(TAG, "Requested invalid range, restarting download from the beginning");
                    FileUtils.deleteQuietly(
                        new File(downloader.getDownloadRequest().getDestination()));
                    DownloadRequester.getInstance()
                        .download(DownloadService.this, downloader.getDownloadRequest());
                  } else {
                    Log.e(TAG, "Download failed");
                    saveDownloadStatus(status);
                    handleFailedDownload(status, downloader.getDownloadRequest());
                  }
                }
                sendDownloadHandledIntent();
                queryDownloadsAsync();
              }
            } catch (InterruptedException e) {
              if (BuildConfig.DEBUG) Log.d(TAG, "DownloadCompletionThread was interrupted");
            } catch (ExecutionException e) {
              e.printStackTrace();
              numberOfDownloads.decrementAndGet();
            }
          }
          if (BuildConfig.DEBUG) Log.d(TAG, "End of downloadCompletionThread");
        }