@Override
  public void onDestroy() {
    if (AppConfig.DEBUG) Log.d(TAG, "Service shutting down");
    isRunning = false;
    updateReport();

    stopForeground(true);
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);

    downloadCompletionThread.interrupt();
    syncExecutor.shutdown();
    schedExecutor.shutdown();
    cancelNotificationUpdater();
    unregisterReceiver(cancelDownloadReceiver);
  }
 /** Is called whenever a FeedMedia is downloaded. */
 private void handleCompletedFeedMediaDownload(DownloadStatus status, DownloadRequest request) {
   if (AppConfig.DEBUG) Log.d(TAG, "Handling completed FeedMedia Download");
   syncExecutor.execute(new MediaHandlerThread(status, request));
 }
 /** Is called whenever a Feed is downloaded */
 private void handleCompletedFeedDownload(DownloadRequest request) {
   if (AppConfig.DEBUG) Log.d(TAG, "Handling completed Feed Download");
   syncExecutor.execute(new FeedSyncThread(request));
 }
    @Override
    public void run() {
      while (isActive) {
        final List<Feed> feeds = collectCompletedRequests();

        if (feeds == null) {
          continue;
        }

        if (BuildConfig.DEBUG) Log.d(TAG, "Bundling " + feeds.size() + " feeds");

        for (Feed feed : feeds) {
          removeDuplicateImages(
              feed); // duplicate images have to removed because the DownloadRequester does not
                     // accept two downloads with the same download URL yet.
        }

        // Save information of feed in DB
        if (dbUpdateFuture != null) {
          try {
            dbUpdateFuture.get();
          } catch (InterruptedException e) {
            e.printStackTrace();
          } catch (ExecutionException e) {
            e.printStackTrace();
          }
        }

        dbUpdateFuture =
            dbService.submit(
                new Runnable() {
                  @Override
                  public void run() {
                    Feed[] savedFeeds =
                        DBTasks.updateFeed(
                            DownloadService.this, feeds.toArray(new Feed[feeds.size()]));

                    for (Feed savedFeed : savedFeeds) {
                      // Download Feed Image if provided and not downloaded
                      if (savedFeed.getImage() != null
                          && savedFeed.getImage().isDownloaded() == false) {
                        if (BuildConfig.DEBUG) Log.d(TAG, "Feed has image; Downloading....");
                        savedFeed.getImage().setOwner(savedFeed);
                        final Feed savedFeedRef = savedFeed;
                        try {
                          requester.downloadImage(DownloadService.this, savedFeedRef.getImage());
                        } catch (DownloadRequestException e) {
                          e.printStackTrace();
                          DBWriter.addDownloadStatus(
                              DownloadService.this,
                              new DownloadStatus(
                                  savedFeedRef.getImage(),
                                  savedFeedRef.getImage().getHumanReadableIdentifier(),
                                  DownloadError.ERROR_REQUEST_ERROR,
                                  false,
                                  e.getMessage()));
                        }
                      }
                      numberOfDownloads.decrementAndGet();
                    }

                    sendDownloadHandledIntent();

                    queryDownloadsAsync();
                  }
                });
      }

      if (dbUpdateFuture != null) {
        try {
          dbUpdateFuture.get();
        } catch (InterruptedException e) {
        } catch (ExecutionException e) {
          e.printStackTrace();
        }
      }

      if (BuildConfig.DEBUG) Log.d(TAG, "Shutting down");
    }
 private void handleFailedDownload(DownloadStatus status, DownloadRequest request) {
   if (BuildConfig.DEBUG) Log.d(TAG, "Handling failed download");
   syncExecutor.execute(new FailedDownloadHandler(status, request));
 }
 /** Is called whenever a Feed-Image is downloaded */
 private void handleCompletedImageDownload(DownloadStatus status, DownloadRequest request) {
   if (BuildConfig.DEBUG) Log.d(TAG, "Handling completed Image Download");
   syncExecutor.execute(new ImageHandlerThread(status, request));
 }