Implemented FeedMedia Download

This commit is contained in:
Daniel Oeh 2012-05-28 13:22:23 +02:00
parent 4caa46e03f
commit 0cecd82550
5 changed files with 79 additions and 2 deletions

View File

@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Podfetcher" default="help">
<!--
<taskdef resource="checkstyletask.properties"
classpath="/home/daniel/bin/checkstyle/checkstyle-5.5-all.jar"/>
<checkstyle config="/home/daniel/bin/checkstyle/sun_checks.xml">
<fileset dir="src" includes="**/*.java"/>
</checkstyle>
-->
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into

View File

@ -1,6 +1,7 @@
package de.podfetcher.activity;
import java.io.File;
import java.util.concurrent.Callable;
import android.net.Uri;
import android.graphics.BitmapFactory;
import com.actionbarsherlock.app.SherlockActivity;
@ -10,16 +11,20 @@ import android.os.Bundle;
import de.podfetcher.feed.*;
import android.util.Log;
import android.content.Intent;
import android.content.Context;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ImageView;
import de.podfetcher.R;
import de.podfetcher.service.DownloadObserver;
import de.podfetcher.storage.DownloadRequester;
/** Displays a single FeedItem and provides various actions */
public class ItemviewActivity extends SherlockActivity {
private static final String TAG = "ItemviewActivity";
private FeedManager manager;
private DownloadRequester requester;
private FeedItem item;
// Widgets
@ -36,6 +41,16 @@ public class ItemviewActivity extends SherlockActivity {
manager = FeedManager.getInstance();
extractFeeditem();
populateUI();
getDownloadStatus();
butDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requester = DownloadRequester.getInstance();
requester.downloadMedia(v.getContext(), item.getMedia());
getDownloadStatus();
}
});
}
/** Extracts FeedItem object the activity is supposed to display */
@ -68,6 +83,41 @@ public class ItemviewActivity extends SherlockActivity {
imgvImage.setImageBitmap(item.getFeed().getImage().getImageBitmap());
}
}
private void getDownloadStatus() {
if(item.getMedia().getFile_url() == null) {
butPlay.setEnabled(false);
butDownload.setEnabled(true);
butRemove.setEnabled(false);
} else {
final DownloadObserver observer = new DownloadObserver(
item.getMedia().getDownloadId(), this);
final Callable client = new Callable() {
public Object call() {
runOnUiThread(new Runnable() {
public void run() {
if(observer.getDone() &&
// Download successful
observer.getResult() == R.string.download_successful) {
butDownload.setEnabled(false);
butPlay.setEnabled(true);
butRemove.setEnabled(true);
} else {
// Download running
butDownload.setEnabled(false);
butPlay.setEnabled(false);
butRemove.setEnabled(false);
}
}
});
return null;
}
};
observer.setClient(client);
observer.start();
}
}
}

View File

@ -136,6 +136,12 @@ public class FeedManager {
return adapter.setImage(image);
}
/** Updates information of an existing FeedMedia object. */
public long setFeedMedia(Context context, FeedMedia media) {
PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setMedia(media);
}
/** Get a Feed by its id */
public Feed getFeed(long id) {
for(Feed f : feeds) {

View File

@ -102,6 +102,11 @@ public class DownloadService extends Service {
FeedImage image = requester.getFeedImage(downloadId);
if(image != null) {
handleCompletedImageDownload(context, image);
} else {
FeedMedia media = requester.getFeedMedia(downloadId);
if (media != null) {
handleCompletedFeedMediaDownload(context, media);
}
}
}
queryDownloads();
@ -131,6 +136,13 @@ public class DownloadService extends Service {
manager.setFeedImage(this, image);
}
/** Is called whenever a FeedMedia is downloaded. */
private void handleCompletedFeedMediaDownload(Context context, FeedMedia media) {
Log.d(TAG, "Handling completed FeedMedia Download");
requester.removeFeedMedia(media);
manager.setFeedMedia(this, media);
}
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {

View File

@ -87,7 +87,7 @@ public class DownloadRequester {
public void downloadMedia(Context context, FeedMedia feedmedia) {
download(context, media, feedmedia,
new File(context.getExternalFilesDir(MEDIA_DOWNLOADPATH), "media-" + media.size()),
new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia)),
true);
}
@ -183,6 +183,14 @@ public class DownloadRequester {
return "image-" + NumberGenerator.generateLong(image.getDownload_url());
}
public String getMediafilePath(Context context, FeedMedia media) {
return context.getExternalFilesDir(MEDIA_DOWNLOADPATH).toString() + "/";
}
public String getMediafilename(FeedMedia media) {
return "media-" + NumberGenerator.generateLong(media.getDownload_url());
}
/* ------------ Methods for communicating with the DownloadService ------------- */
private Messenger mService = null;