private void download(InputStream is, String destinationFile) {
    try {
      double currentSize = 0;
      OutputStream os = new FileOutputStream(destinationFile);

      byte[] b = new byte[4096];
      int length;
      this.setProgress(0);

      while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
        currentSize += length;
        setProgress((int) ((currentSize / fileSize) * 100));
      }

      is.close();
      os.close();
    } catch (FileNotFoundException e) {
      java.io.File file = new java.io.File(MAMUtil.getTempDownloadPath());
      file.mkdirs();
      download(is, destinationFile);
    } catch (Exception e) {
      MAMUtil.writeLog(e);
      e.printStackTrace();
    }
  }
 private void downloadFileWithStream(String fileName) {
   try {
     InputStream is = downloadFile(fileName);
     download(is, MAMUtil.getTempDownloadPath() + fileName);
     FileUtils.moveFile(
         new java.io.File(MAMUtil.getTempDownloadPath() + fileName),
         new java.io.File(MAMUtil.getMusicPath() + fileName));
   } catch (Exception e) {
     MAMUtil.writeLog(e);
     e.printStackTrace();
   }
 }
 public GoogleDriveDownloadTask(ArrayList<String> fileList) {
   ArrayList<String> fileToDownloadList = new ArrayList<String>();
   int fileToDownload = 0;
   for (String fileName : fileList) {
     java.io.File song = new java.io.File(MAMUtil.getMusicPath() + fileName + ".mp3");
     if (!song.exists()) {
       fileToDownloadList.add(fileName);
       fileToDownload++;
     }
   }
   this.fileList = fileToDownloadList;
   this.totalFileNumber = fileToDownload;
 }
 public GoogleDriveDownloadTask(String fileName) {
   if (DriveUtil.service == null) {
     try {
       DriveUtil.getDriveService();
     } catch (GeneralSecurityException | IOException | URISyntaxException e) {
       MAMUtil.writeLog(e);
       e.printStackTrace();
     }
   }
   ArrayList<String> fileList = new ArrayList<String>();
   fileList.add(fileName);
   this.fileList = fileList;
   this.totalFileNumber = 1;
 }
  private InputStream downloadFile(String fileName) throws IOException {
    File file = DriveUtil.getFileByName(fileName);

    if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0)
      try {
        fileSize = file.getFileSize();
        return DriveUtil.service.files().get(file.getId()).executeMediaAsInputStream();
      } catch (IOException e) {
        e.printStackTrace();
        MAMUtil.writeLog(e);
        return null;
      }

    return null;
  }
  @Override
  protected Object doInBackground() throws Exception {
    ReleaseNotifierDialog.ovaReleased.clear();
    ReleaseNotifierDialog.filmReleased.clear();
    String oggi = MAMUtil.today();
    GregorianCalendar today = MAMUtil.getDate(oggi);
    GregorianCalendar lastControlDate = null;
    if (!AnimeIndex.appProp.getProperty("Date_Release").equalsIgnoreCase("none"))
      lastControlDate = MAMUtil.getDate(AnimeIndex.appProp.getProperty("Date_Release"));
    GregorianCalendar animeDate = null;
    Object[] ovaArray = AnimeIndex.ovaModel.toArray();
    for (int i = 0; i < ovaArray.length; i++) {
      String name = (String) ovaArray[i];
      AnimeData data = AnimeIndex.ovaMap.get(name);
      String releaseDate = data.getFinishDate();
      String secondaryReleaseDate = data.getReleaseDate();

      if (releaseDate.contains("?") || today.before(MAMUtil.getDate(releaseDate))) {
        if (!secondaryReleaseDate.contains("?")) animeDate = MAMUtil.getDate(secondaryReleaseDate);
      } else animeDate = MAMUtil.getDate(releaseDate);
      if (animeDate != null) {
        if (AnimeIndex.openReleaseDialog == true)
          if (AnimeIndex.exitDateMap.containsKey(name))
            if (MAMUtil.getDate(AnimeIndex.exitDateMap.get(name)).equals(today))
              if (animeDate.before(today) || animeDate.equals(today)) {
                ReleaseNotifierDialog.ovaReleased.addElement(name);
                if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                  AnimeData newData =
                      new AnimeData(
                          data.getCurrentEpisode(),
                          data.getTotalEpisode(),
                          data.getFansub(),
                          data.getNote(),
                          data.getImageName(),
                          "Rilasciato",
                          data.getId(),
                          data.getLinkName(),
                          data.getLink(),
                          data.getAnimeType(),
                          data.getReleaseDate(),
                          data.getFinishDate(),
                          data.getDurationEp(),
                          data.getBd());
                  AnimeIndex.ovaMap.put(name, newData);
                }
              }

        if (lastControlDate != null) {
          if (animeDate.after(lastControlDate)) {
            if (AnimeIndex.exitDateMap.containsKey(name)) {
              if (MAMUtil.getDate(AnimeIndex.exitDateMap.get(name)).before(animeDate)) {
                if (animeDate.before(today) || animeDate.equals(today)) {
                  ReleaseNotifierDialog.ovaReleased.addElement(name);
                  AnimeIndex.exitDateMap.put(name, oggi);
                  if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                    AnimeData newData =
                        new AnimeData(
                            data.getCurrentEpisode(),
                            data.getTotalEpisode(),
                            data.getFansub(),
                            data.getNote(),
                            data.getImageName(),
                            "Rilasciato",
                            data.getId(),
                            data.getLinkName(),
                            data.getLink(),
                            data.getAnimeType(),
                            data.getReleaseDate(),
                            data.getFinishDate(),
                            data.getDurationEp(),
                            data.getBd());
                    AnimeIndex.ovaMap.put(name, newData);
                  }
                }
              }
            } else if (animeDate.before(today) || animeDate.equals(today)) {
              ReleaseNotifierDialog.ovaReleased.addElement(name);
              AnimeIndex.exitDateMap.put(name, oggi);
              if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                AnimeData newData =
                    new AnimeData(
                        data.getCurrentEpisode(),
                        data.getTotalEpisode(),
                        data.getFansub(),
                        data.getNote(),
                        data.getImageName(),
                        "Rilasciato",
                        data.getId(),
                        data.getLinkName(),
                        data.getLink(),
                        data.getAnimeType(),
                        data.getReleaseDate(),
                        data.getFinishDate(),
                        data.getDurationEp(),
                        data.getBd());
                AnimeIndex.ovaMap.put(name, newData);
              }
            }
          }
        } else if (AnimeIndex.exitDateMap.containsKey(name)) {
          if (MAMUtil.getDate(AnimeIndex.exitDateMap.get(name)).before(animeDate)) {
            if (animeDate.before(today) || animeDate.equals(today)) {
              ReleaseNotifierDialog.ovaReleased.addElement(name);
              AnimeIndex.exitDateMap.put(name, oggi);
              if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                AnimeData newData =
                    new AnimeData(
                        data.getCurrentEpisode(),
                        data.getTotalEpisode(),
                        data.getFansub(),
                        data.getNote(),
                        data.getImageName(),
                        "Rilasciato",
                        data.getId(),
                        data.getLinkName(),
                        data.getLink(),
                        data.getAnimeType(),
                        data.getReleaseDate(),
                        data.getFinishDate(),
                        data.getDurationEp(),
                        data.getBd());
                AnimeIndex.ovaMap.put(name, newData);
              }
            }
          }
        } else if (animeDate.before(today) || animeDate.equals(today)) {
          ReleaseNotifierDialog.ovaReleased.addElement(name);
          AnimeIndex.exitDateMap.put(name, oggi);
          if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
            AnimeData newData =
                new AnimeData(
                    data.getCurrentEpisode(),
                    data.getTotalEpisode(),
                    data.getFansub(),
                    data.getNote(),
                    data.getImageName(),
                    "Rilasciato",
                    data.getId(),
                    data.getLinkName(),
                    data.getLink(),
                    data.getAnimeType(),
                    data.getReleaseDate(),
                    data.getFinishDate(),
                    data.getDurationEp(),
                    data.getBd());
            AnimeIndex.ovaMap.put(name, newData);
          }
        }
      }
    }

    animeDate = null;
    Object[] filmArray = AnimeIndex.filmModel.toArray();
    for (int i = 0; i < filmArray.length; i++) {
      String name = (String) filmArray[i];
      AnimeData data = AnimeIndex.filmMap.get(name);
      String releaseDate = data.getFinishDate();
      String secondaryReleaseDate = data.getReleaseDate();

      if (releaseDate.contains("?") || today.before(MAMUtil.getDate(releaseDate))) {
        if (!secondaryReleaseDate.contains("?")) animeDate = MAMUtil.getDate(secondaryReleaseDate);
      } else animeDate = MAMUtil.getDate(releaseDate);
      if (animeDate != null) {
        if (AnimeIndex.openReleaseDialog == true)
          if (AnimeIndex.exitDateMap.containsKey(name))
            if (MAMUtil.getDate(AnimeIndex.exitDateMap.get(name)).equals(today))
              if (animeDate.before(today) || animeDate.equals(today)) {
                ReleaseNotifierDialog.filmReleased.addElement(name);
                if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                  AnimeData newData =
                      new AnimeData(
                          data.getCurrentEpisode(),
                          data.getTotalEpisode(),
                          data.getFansub(),
                          data.getNote(),
                          data.getImageName(),
                          "Rilasciato",
                          data.getId(),
                          data.getLinkName(),
                          data.getLink(),
                          data.getAnimeType(),
                          data.getReleaseDate(),
                          data.getFinishDate(),
                          data.getDurationEp(),
                          data.getBd());
                  AnimeIndex.filmMap.put(name, newData);
                }
              }

        if (lastControlDate != null) {
          if (animeDate.after(lastControlDate)) {
            if (AnimeIndex.exitDateMap.containsKey(name)) {
              if (MAMUtil.getDate(AnimeIndex.exitDateMap.get(name)).before(animeDate)) {
                if (animeDate.before(today) || animeDate.equals(today)) {
                  ReleaseNotifierDialog.filmReleased.addElement(name);
                  AnimeIndex.exitDateMap.put(name, oggi);
                  if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                    AnimeData newData =
                        new AnimeData(
                            data.getCurrentEpisode(),
                            data.getTotalEpisode(),
                            data.getFansub(),
                            data.getNote(),
                            data.getImageName(),
                            "Rilasciato",
                            data.getId(),
                            data.getLinkName(),
                            data.getLink(),
                            data.getAnimeType(),
                            data.getReleaseDate(),
                            data.getFinishDate(),
                            data.getDurationEp(),
                            data.getBd());
                    AnimeIndex.filmMap.put(name, newData);
                  }
                }
              }
            } else if (animeDate.before(today) || animeDate.equals(today)) {
              ReleaseNotifierDialog.filmReleased.addElement(name);
              AnimeIndex.exitDateMap.put(name, oggi);
              if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                AnimeData newData =
                    new AnimeData(
                        data.getCurrentEpisode(),
                        data.getTotalEpisode(),
                        data.getFansub(),
                        data.getNote(),
                        data.getImageName(),
                        "Rilasciato",
                        data.getId(),
                        data.getLinkName(),
                        data.getLink(),
                        data.getAnimeType(),
                        data.getReleaseDate(),
                        data.getFinishDate(),
                        data.getDurationEp(),
                        data.getBd());
                AnimeIndex.filmMap.put(name, newData);
              }
            }
          }
        } else if (AnimeIndex.exitDateMap.containsKey(name)) {
          if (MAMUtil.getDate(AnimeIndex.exitDateMap.get(name)).before(animeDate)) {
            if (animeDate.before(today) || animeDate.equals(today)) {
              ReleaseNotifierDialog.filmReleased.addElement(name);
              AnimeIndex.exitDateMap.put(name, oggi);
              if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
                AnimeData newData =
                    new AnimeData(
                        data.getCurrentEpisode(),
                        data.getTotalEpisode(),
                        data.getFansub(),
                        data.getNote(),
                        data.getImageName(),
                        "Rilasciato",
                        data.getId(),
                        data.getLinkName(),
                        data.getLink(),
                        data.getAnimeType(),
                        data.getReleaseDate(),
                        data.getFinishDate(),
                        data.getDurationEp(),
                        data.getBd());
                AnimeIndex.filmMap.put(name, newData);
              }
            }
          }
        } else if (animeDate.before(today) || animeDate.equals(today)) {
          ReleaseNotifierDialog.filmReleased.addElement(name);
          AnimeIndex.exitDateMap.put(name, oggi);
          if (!data.getDay().equalsIgnoreCase("Rilasciato")) {
            AnimeData newData =
                new AnimeData(
                    data.getCurrentEpisode(),
                    data.getTotalEpisode(),
                    data.getFansub(),
                    data.getNote(),
                    data.getImageName(),
                    "Rilasciato",
                    data.getId(),
                    data.getLinkName(),
                    data.getLink(),
                    data.getAnimeType(),
                    data.getReleaseDate(),
                    data.getFinishDate(),
                    data.getDurationEp(),
                    data.getBd());
            AnimeIndex.filmMap.put(name, newData);
          }
        }
      }
    }
    if (ReleaseNotifierDialog.ovaReleased.isEmpty()) {
      ReleaseNotifierDialog.ovaReleased.addElement("Nessun Anime Rilasciato");
      enableOav = false;
    } else enableOav = true;
    if (ReleaseNotifierDialog.filmReleased.isEmpty()) {
      ReleaseNotifierDialog.filmReleased.addElement("Nessun Anime Rilasciato");
      enableFilm = false;
    } else enableFilm = true;
    return null;
  }