Ejemplo n.º 1
0
  // Prepare the download. Returns true if the chapter is already downloaded
  private boolean prepareDownload(Download download) {
    // If the chapter is already queued, don't add it again
    for (Download queuedDownload : queue) {
      if (download.chapter.id.equals(queuedDownload.chapter.id)) return true;
    }

    // Add the directory to the download object for future access
    download.directory = getAbsoluteChapterDirectory(download);

    // If the directory doesn't exist, the chapter isn't downloaded.
    if (!download.directory.exists()) {
      return false;
    }

    // If the page list doesn't exist, the chapter isn't downloaded
    List<Page> savedPages = getSavedPageList(download);
    if (savedPages == null) return false;

    // Add the page list to the download object for future access
    download.pages = savedPages;

    // If the number of files matches the number of pages, the chapter is downloaded.
    // We have the index file, so we check one file more
    return isChapterDownloaded(download.directory, download.pages);
  }
Ejemplo n.º 2
0
 public void stopDownloads() {
   destroySubscriptions();
   for (Download download : queue) {
     if (download.getStatus() == Download.DOWNLOADING) {
       download.setStatus(Download.ERROR);
     }
   }
 }
Ejemplo n.º 3
0
  public boolean startDownloads() {
    if (queue.isEmpty()) return false;

    boolean hasPendingDownloads = false;
    if (downloadsSubscription == null) initializeSubscriptions();

    for (Download download : queue) {
      if (download.getStatus() != Download.DOWNLOADED) {
        if (download.getStatus() != Download.QUEUE) download.setStatus(Download.QUEUE);
        if (!hasPendingDownloads) hasPendingDownloads = true;
        downloadsQueueSubject.onNext(download);
      }
    }
    return hasPendingDownloads;
  }
Ejemplo n.º 4
0
 private void checkDownloadIsSuccessful(final Download download) {
   int actualProgress = 0;
   int status = Download.DOWNLOADED;
   // If any page has an error, the download result will be error
   for (Page page : download.pages) {
     actualProgress += page.getProgress();
     if (page.getStatus() != Page.READY) status = Download.ERROR;
   }
   // Ensure that the chapter folder has all the images
   if (!isChapterDownloaded(download.directory, download.pages)) {
     status = Download.ERROR;
   }
   download.totalProgress = actualProgress;
   download.setStatus(status);
   // Delete successful downloads from queue after notifying
   if (status == Download.DOWNLOADED) {
     queue.remove(download);
   }
 }
Ejemplo n.º 5
0
  // Download the entire chapter
  private Observable<Download> downloadChapter(Download download) {
    try {
      DiskUtils.createDirectory(download.directory);
    } catch (IOException e) {
      return Observable.error(e);
    }

    Observable<List<Page>> pageListObservable =
        download.pages == null
            ?
            // Pull page list from network and add them to download object
            download
                .source
                .pullPageListFromNetwork(download.chapter.url)
                .doOnNext(pages -> download.pages = pages)
                .doOnNext(pages -> savePageList(download))
            :
            // Or if the page list already exists, start from the file
            Observable.just(download.pages);

    return pageListObservable
        .subscribeOn(Schedulers.io())
        .doOnNext(
            pages -> {
              download.downloadedImages = 0;
              download.setStatus(Download.DOWNLOADING);
            })
        // Get all the URLs to the source images, fetch pages if necessary
        .flatMap(download.source::getAllImageUrlsFromPageList)
        // Start downloading images, consider we can have downloaded images already
        .concatMap(page -> getOrDownloadImage(page, download))
        // Do after download completes
        .doOnCompleted(() -> onDownloadCompleted(download))
        .toList()
        .map(pages -> download)
        // If the page list threw, it will resume here
        .onErrorResumeNext(
            error -> {
              download.setStatus(Download.ERROR);
              return Observable.just(download);
            });
  }
Ejemplo n.º 6
0
 public boolean areAllDownloadsFinished() {
   for (Download download : queue) {
     if (download.getStatus() <= Download.DOWNLOADING) return false;
   }
   return true;
 }