protected byte[] readSourcePacket() throws IOException {
    java.sql.Timestamp lastTimestamp = null;
    java.sql.Timestamp crrntTimestamp = null;

    byte[] packet = dbReader.NextPacket();
    crrntTimestamp = dbReader.GetTimestamp();
    if (packet == null || crrntTimestamp == null) {
      throw new IOException("database packet read failed");
    }
    lastTimestamp = crrntTimestamp;

    int sleep = (int) (crrntTimestamp.getTime() - lastTimestamp.getTime());
    if (sleep > 0) {
      message("Sleeping for: " + sleep);
      try {
        Thread.currentThread().sleep(sleep);
      } catch (Exception e) {
      }
    }

    return packet;
  }
예제 #2
0
    @Override
    public void run() {
      FeedImage image = DBReader.getFeedImage(DownloadService.this, request.getFeedfileId());
      if (image == null) {
        throw new IllegalStateException("Could not find downloaded image in database");
      }

      image.setFile_url(request.getDestination());
      image.setDownloaded(true);

      saveDownloadStatus(status);
      sendDownloadHandledIntent();
      DBWriter.setFeedImage(DownloadService.this, image);
      numberOfDownloads.decrementAndGet();
      queryDownloadsAsync();
    }
예제 #3
0
 @Override
 public void run() {
   if (request.isDeleteOnFailure()) {
     if (BuildConfig.DEBUG) Log.d(TAG, "Ignoring failed download, deleteOnFailure=true");
   } else {
     File dest = new File(request.getDestination());
     if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
       Log.d(TAG, "File has been partially downloaded. Writing file url");
       FeedMedia media = DBReader.getFeedMedia(DownloadService.this, request.getFeedfileId());
       media.setFile_url(request.getDestination());
       try {
         DBWriter.setFeedMedia(DownloadService.this, media).get();
       } catch (InterruptedException e) {
         e.printStackTrace();
       } catch (ExecutionException e) {
         e.printStackTrace();
       }
     }
   }
 }
예제 #4
0
    @Override
    public void run() {
      FeedMedia media = DBReader.getFeedMedia(DownloadService.this, request.getFeedfileId());
      if (media == null) {
        throw new IllegalStateException("Could not find downloaded media object in database");
      }
      boolean chaptersRead = false;
      media.setDownloaded(true);
      media.setFile_url(request.getDestination());

      // Get duration
      MediaMetadataRetriever mmr = null;
      try {
        mmr = new MediaMetadataRetriever();
        mmr.setDataSource(media.getFile_url());
        String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        media.setDuration(Integer.parseInt(durationStr));
        if (BuildConfig.DEBUG) Log.d(TAG, "Duration of file is " + media.getDuration());
      } catch (NumberFormatException e) {
        e.printStackTrace();
      } catch (RuntimeException e) {
        e.printStackTrace();
      } finally {
        if (mmr != null) {
          mmr.release();
        }
      }

      if (media.getItem().getChapters() == null) {
        ChapterUtils.loadChaptersFromFileUrl(media);
        if (media.getItem().getChapters() != null) {
          chaptersRead = true;
        }
      }

      try {
        if (chaptersRead) {
          DBWriter.setFeedItem(DownloadService.this, media.getItem()).get();
        }
        DBWriter.setFeedMedia(DownloadService.this, media).get();
        if (!DBTasks.isInQueue(DownloadService.this, media.getItem().getId())) {
          DBWriter.addQueueItem(DownloadService.this, media.getItem().getId()).get();
        }
      } catch (ExecutionException e) {
        e.printStackTrace();
        status =
            new DownloadStatus(
                media,
                media.getEpisodeTitle(),
                DownloadError.ERROR_DB_ACCESS_ERROR,
                false,
                e.getMessage());
      } catch (InterruptedException e) {
        e.printStackTrace();
        status =
            new DownloadStatus(
                media,
                media.getEpisodeTitle(),
                DownloadError.ERROR_DB_ACCESS_ERROR,
                false,
                e.getMessage());
      }

      saveDownloadStatus(status);
      sendDownloadHandledIntent();

      numberOfDownloads.decrementAndGet();
      queryDownloadsAsync();
    }
 protected void closeSource() {
   dbReader.Close();
 }
 protected void openSource() throws IOException {
   if (!dbReader.Connect()) {
     throw new IOException("database connection failed");
   }
 }