Skip to content

Commit

Permalink
tinkered with downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
thasmin committed May 23, 2012
1 parent afc45e0 commit 50f837e
Showing 1 changed file with 45 additions and 25 deletions.
70 changes: 45 additions & 25 deletions src/com/axelby/podax/PodcastDownloader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.axelby.podax;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -33,7 +34,7 @@ class PodcastDownloader {
_context = context;
}

public void download() {
public synchronized void download() {
// if we've needed to interrupt 4 times, something may be wrong
--_interruptCount;
if (_thread != null && _interruptCount > 0) {
Expand Down Expand Up @@ -62,16 +63,18 @@ public void run() {
Log.d("Podax", "starting podcast downloader on thread " + Thread.currentThread().getId());
Cursor cursor = null;
try {
Uri uri = Uri.withAppendedPath(PodcastProvider.URI, "to_download");
String[] projection = {
PodcastProvider.COLUMN_ID,
PodcastProvider.COLUMN_TITLE,
PodcastProvider.COLUMN_MEDIA_URL,
PodcastProvider.COLUMN_FILE_SIZE,
};
cursor = _context.getContentResolver().query(uri, projection, null, null, null);
cursor = _context.getContentResolver().query(PodcastProvider.QUEUE_URI, projection, null, null, null);
while (cursor.moveToNext()) {
InputStream instream = null;
PodcastCursor podcast = new PodcastCursor(_context, cursor);
if (podcast.isDownloaded())
continue;

File mediaFile = new File(podcast.getFilename());

try {
Expand All @@ -90,6 +93,7 @@ public void run() {
// response code 206 means partial content and range header worked
boolean append = false;
if (c.getResponseCode() == 206) {
// make sure there's more data to download
if (c.getContentLength() <= 0) {
podcast.setFileSize(mediaFile.length());
continue;
Expand All @@ -99,33 +103,21 @@ public void run() {
podcast.setFileSize(c.getContentLength());
}

FileOutputStream outstream = new FileOutputStream(mediaFile, append);
instream = c.getInputStream();
int read;
byte[] b = new byte[1024*64];
while (!Thread.currentThread().isInterrupted() && (read = instream.read(b, 0, b.length)) != -1) {
outstream.write(b, 0, read);
if (!downloadFile(c, mediaFile, append))
continue;

if (mediaFile.length() == c.getContentLength()) {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(podcast.getFilename());
mp.prepare();
podcast.setDuration(mp.getDuration());
mp.release();
}
instream.close();
outstream.close();

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(podcast.getFilename());
mp.prepare();
podcast.setDuration(mp.getDuration());
mp.release();

Log.d("Podax", "Done downloading " + podcast.getTitle());
} catch (Exception e) {
Log.e("Podax", "Exception while downloading " + podcast.getTitle(), e);
removeDownloadNotification();

try {
if (instream != null)
instream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
break;
}
}
Expand All @@ -136,6 +128,25 @@ public void run() {
_thread = null;
}
}

private boolean downloadFile(HttpURLConnection conn, File file, boolean append) {
FileOutputStream outstream = null;
InputStream instream = null;
try {
outstream = new FileOutputStream(file, append);
instream = conn.getInputStream();
int read;
byte[] b = new byte[1024*64];
while (!Thread.currentThread().isInterrupted() && (read = instream.read(b, 0, b.length)) != -1)
outstream.write(b, 0, read);
} catch (Exception e) {
return false;
} finally {
close(outstream);
close(instream);
}
return file.length() == conn.getContentLength();
}
};

private void verifyDownloadedFiles() {
Expand Down Expand Up @@ -164,6 +175,15 @@ private void verifyDownloadedFiles() {
}
}

public static void close(Closeable c) {
if (c == null)
return;
try {
c.close();
} catch (IOException e) {
}
}

void updateDownloadNotification(PodcastCursor podcast, long downloaded) {
Intent notificationIntent = MainActivity.getSubscriptionIntent(_context);
PendingIntent contentIntent = PendingIntent.getActivity(_context, 0, notificationIntent, 0);
Expand Down

0 comments on commit 50f837e

Please sign in to comment.