Created Playback Service
This commit is contained in:
parent
d92de110df
commit
d98fa57468
|
@ -29,5 +29,6 @@
|
||||||
<activity android:name="de.podfetcher.activity.ItemviewActivity"/>
|
<activity android:name="de.podfetcher.activity.ItemviewActivity"/>
|
||||||
|
|
||||||
<service android:enabled="true" android:name="de.podfetcher.service.DownloadService" />
|
<service android:enabled="true" android:name="de.podfetcher.service.DownloadService" />
|
||||||
|
<service android:enabled="true" android:name="de.podfetcher.service.PlaybackService" />
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
|
@ -17,6 +17,7 @@ import android.widget.TextView;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import de.podfetcher.R;
|
import de.podfetcher.R;
|
||||||
import de.podfetcher.service.DownloadObserver;
|
import de.podfetcher.service.DownloadObserver;
|
||||||
|
import de.podfetcher.service.PlaybackService;
|
||||||
import de.podfetcher.storage.DownloadRequester;
|
import de.podfetcher.storage.DownloadRequester;
|
||||||
import de.podfetcher.fragment.FeedlistFragment;
|
import de.podfetcher.fragment.FeedlistFragment;
|
||||||
|
|
||||||
|
@ -52,6 +53,16 @@ public class ItemviewActivity extends SherlockActivity {
|
||||||
getDownloadStatus();
|
getDownloadStatus();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
butPlay.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Intent launchIntent = new Intent(v.getContext(), PlaybackService.class);
|
||||||
|
launchIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, item.getMedia().getId());
|
||||||
|
launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, item.getFeed().getId());
|
||||||
|
v.getContext().startService(launchIntent);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Extracts FeedItem object the activity is supposed to display */
|
/** Extracts FeedItem object the activity is supposed to display */
|
||||||
|
|
|
@ -17,21 +17,21 @@ import android.util.Log;
|
||||||
* */
|
* */
|
||||||
public class FeedManager {
|
public class FeedManager {
|
||||||
private static final String TAG = "FeedManager";
|
private static final String TAG = "FeedManager";
|
||||||
|
|
||||||
private static FeedManager singleton;
|
private static FeedManager singleton;
|
||||||
|
|
||||||
private ArrayList<Feed> feeds;
|
private ArrayList<Feed> feeds;
|
||||||
private ArrayList<FeedCategory> categories;
|
private ArrayList<FeedCategory> categories;
|
||||||
private DownloadRequester requester;
|
private DownloadRequester requester;
|
||||||
|
|
||||||
|
|
||||||
private FeedManager() {
|
private FeedManager() {
|
||||||
feeds = new ArrayList<Feed>();
|
feeds = new ArrayList<Feed>();
|
||||||
categories = new ArrayList<FeedCategory>();
|
categories = new ArrayList<FeedCategory>();
|
||||||
requester = DownloadRequester.getInstance();
|
requester = DownloadRequester.getInstance();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FeedManager getInstance(){
|
public static FeedManager getInstance(){
|
||||||
if(singleton == null) {
|
if(singleton == null) {
|
||||||
singleton = new FeedManager();
|
singleton = new FeedManager();
|
||||||
|
@ -53,7 +53,7 @@ public class FeedManager {
|
||||||
setFeedItem(context, item);
|
setFeedItem(context, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Adds a new Feeditem if its not in the list */
|
/** Adds a new Feeditem if its not in the list */
|
||||||
public void addFeedItem(Context context, FeedItem item) {
|
public void addFeedItem(Context context, FeedItem item) {
|
||||||
|
@ -129,16 +129,16 @@ public class FeedManager {
|
||||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||||
return adapter.setFeedItem(item);
|
return adapter.setFeedItem(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Updates information of an existing FeedImage */
|
/** Updates information of an existing FeedImage */
|
||||||
public long setFeedImage(Context context, FeedImage image) {
|
public long setFeedImage(Context context, FeedImage image) {
|
||||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||||
return adapter.setImage(image);
|
return adapter.setImage(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Updates information of an existing FeedMedia object. */
|
/** Updates information of an existing FeedMedia object. */
|
||||||
public long setFeedMedia(Context context, FeedMedia media) {
|
public long setFeedMedia(Context context, FeedMedia media) {
|
||||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||||
return adapter.setMedia(media);
|
return adapter.setMedia(media);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,28 +165,39 @@ public class FeedManager {
|
||||||
|
|
||||||
/** Get a Feed Item by its id and its feed*/
|
/** Get a Feed Item by its id and its feed*/
|
||||||
public FeedItem getFeedItem(long id, Feed feed) {
|
public FeedItem getFeedItem(long id, Feed feed) {
|
||||||
for(FeedItem item : feed.getItems()) {
|
for(FeedItem item : feed.getItems()) {
|
||||||
if(item.getId() == id) {
|
if(item.getId() == id) {
|
||||||
return item;
|
return item;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Log.w(TAG, "Couldn't find FeedItem with id " + id);
|
}
|
||||||
|
Log.e(TAG, "Couldn't find FeedItem with id " + id);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get a FeedMedia object by the id of the Media object and the feed object */
|
||||||
|
public FeedMedia getFeedMedia(long id, Feed feed) {
|
||||||
|
for (FeedItem item : feed.getItems()) {
|
||||||
|
if(item.getMedia().getId() == id) {
|
||||||
|
return item.getMedia();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log.e(TAG, "Couldn't find FeedMedia with id " + id);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/** Reads the database */
|
/** Reads the database */
|
||||||
public void loadDBData(Context context) {
|
public void loadDBData(Context context) {
|
||||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||||
updateArrays(context);
|
updateArrays(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void updateArrays(Context context) {
|
public void updateArrays(Context context) {
|
||||||
feeds.clear();
|
feeds.clear();
|
||||||
categories.clear();
|
categories.clear();
|
||||||
extractFeedlistFromCursor(context);
|
extractFeedlistFromCursor(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void extractFeedlistFromCursor(Context context) {
|
private void extractFeedlistFromCursor(Context context) {
|
||||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||||
adapter.open();
|
adapter.open();
|
||||||
|
@ -194,7 +205,7 @@ public class FeedManager {
|
||||||
if(feedlistCursor.moveToFirst()) {
|
if(feedlistCursor.moveToFirst()) {
|
||||||
do {
|
do {
|
||||||
Feed feed = new Feed();
|
Feed feed = new Feed();
|
||||||
|
|
||||||
feed.id = feedlistCursor.getLong(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_ID));
|
feed.id = feedlistCursor.getLong(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_ID));
|
||||||
feed.setTitle(feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_TITLE)));
|
feed.setTitle(feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_TITLE)));
|
||||||
feed.setLink(feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_LINK)));
|
feed.setLink(feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_LINK)));
|
||||||
|
@ -202,17 +213,17 @@ public class FeedManager {
|
||||||
feed.setImage(adapter.getFeedImage(feedlistCursor.getLong(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_IMAGE))));
|
feed.setImage(adapter.getFeedImage(feedlistCursor.getLong(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_IMAGE))));
|
||||||
feed.file_url = feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_FILE_URL));
|
feed.file_url = feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_FILE_URL));
|
||||||
feed.download_url = feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL));
|
feed.download_url = feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL));
|
||||||
|
|
||||||
// Get FeedItem-Object
|
// Get FeedItem-Object
|
||||||
Cursor itemlistCursor = adapter.getAllItemsOfFeedCursor(feed);
|
Cursor itemlistCursor = adapter.getAllItemsOfFeedCursor(feed);
|
||||||
feed.setItems(extractFeedItemsFromCursor(context, feed, itemlistCursor));
|
feed.setItems(extractFeedItemsFromCursor(context, feed, itemlistCursor));
|
||||||
|
|
||||||
feeds.add(feed);
|
feeds.add(feed);
|
||||||
}while(feedlistCursor.moveToNext());
|
}while(feedlistCursor.moveToNext());
|
||||||
}
|
}
|
||||||
adapter.close();
|
adapter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<FeedItem> extractFeedItemsFromCursor(Context context, Feed feed, Cursor itemlistCursor) {
|
private ArrayList<FeedItem> extractFeedItemsFromCursor(Context context, Feed feed, Cursor itemlistCursor) {
|
||||||
ArrayList<FeedItem> items = new ArrayList<FeedItem>();
|
ArrayList<FeedItem> items = new ArrayList<FeedItem>();
|
||||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||||
|
@ -220,7 +231,7 @@ public class FeedManager {
|
||||||
if(itemlistCursor.moveToFirst()) {
|
if(itemlistCursor.moveToFirst()) {
|
||||||
do {
|
do {
|
||||||
FeedItem item = new FeedItem();
|
FeedItem item = new FeedItem();
|
||||||
|
|
||||||
item.id = itemlistCursor.getLong(itemlistCursor.getColumnIndex(PodDBAdapter.KEY_ID));
|
item.id = itemlistCursor.getLong(itemlistCursor.getColumnIndex(PodDBAdapter.KEY_ID));
|
||||||
item.setFeed(feed);
|
item.setFeed(feed);
|
||||||
item.setTitle(itemlistCursor.getString(itemlistCursor.getColumnIndex(PodDBAdapter.KEY_TITLE)));
|
item.setTitle(itemlistCursor.getString(itemlistCursor.getColumnIndex(PodDBAdapter.KEY_TITLE)));
|
||||||
|
@ -231,7 +242,7 @@ public class FeedManager {
|
||||||
itemlistCursor.getLong(
|
itemlistCursor.getLong(
|
||||||
itemlistCursor.getColumnIndex(PodDBAdapter.KEY_MEDIA)), item));
|
itemlistCursor.getColumnIndex(PodDBAdapter.KEY_MEDIA)), item));
|
||||||
item.setRead((itemlistCursor.getInt(itemlistCursor.getColumnIndex(PodDBAdapter.KEY_READ)) > 0) ? true : false);
|
item.setRead((itemlistCursor.getInt(itemlistCursor.getColumnIndex(PodDBAdapter.KEY_READ)) > 0) ? true : false);
|
||||||
|
|
||||||
items.add(item);
|
items.add(item);
|
||||||
} while(itemlistCursor.moveToNext());
|
} while(itemlistCursor.moveToNext());
|
||||||
}
|
}
|
||||||
|
@ -242,8 +253,8 @@ public class FeedManager {
|
||||||
public ArrayList<Feed> getFeeds() {
|
public ArrayList<Feed> getFeeds() {
|
||||||
return feeds;
|
return feeds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ import android.util.Log;
|
||||||
|
|
||||||
|
|
||||||
public class FeedlistFragment extends SherlockListFragment {
|
public class FeedlistFragment extends SherlockListFragment {
|
||||||
private static final String TAG = "FeedlistActivity";
|
private static final String TAG = "FeedlistFragment";
|
||||||
public static final String EXTRA_SELECTED_FEED = "extra.de.podfetcher.activity.selected_feed";
|
public static final String EXTRA_SELECTED_FEED = "extra.de.podfetcher.activity.selected_feed";
|
||||||
|
|
||||||
private FeedManager manager;
|
private FeedManager manager;
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
package de.podfetcher.service;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import android.app.Service;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.media.MediaPlayer;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.IBinder;
|
||||||
|
|
||||||
|
import de.podfetcher.feed.FeedMedia;
|
||||||
|
import de.podfetcher.feed.Feed;
|
||||||
|
import de.podfetcher.feed.FeedManager;
|
||||||
|
|
||||||
|
/** Controls the MediaPlayer that plays a FeedMedia-file */
|
||||||
|
public class PlaybackService extends Service {
|
||||||
|
/** Logging tag */
|
||||||
|
private static final String TAG = "PlaybackService";
|
||||||
|
/** Contains the id of the FeedMedia object. */
|
||||||
|
public static final String EXTRA_MEDIA_ID = "extra.de.podfetcher.service.mediaId";
|
||||||
|
/** Contains the id of the Feed object of the FeedMedia. */
|
||||||
|
public static final String EXTRA_FEED_ID = "extra.de.podfetcher.service.feedId";
|
||||||
|
|
||||||
|
private MediaPlayer player;
|
||||||
|
private FeedMedia media;
|
||||||
|
private Feed feed;
|
||||||
|
private FeedManager manager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
Log.d(TAG, "Service created.");
|
||||||
|
|
||||||
|
manager = FeedManager.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
if (player != null) {
|
||||||
|
player.stop();
|
||||||
|
}
|
||||||
|
long mediaId = intent.getLongExtra(EXTRA_MEDIA_ID, -1);
|
||||||
|
long feedId = intent.getLongExtra(EXTRA_FEED_ID, -1);
|
||||||
|
if (mediaId == -1 || feedId == -1) {
|
||||||
|
Log.e(TAG, "Media ID or Feed ID wasn't provided to the Service.");
|
||||||
|
} else {
|
||||||
|
feed = manager.getFeed(feedId);
|
||||||
|
media = manager.getFeedMedia(mediaId, feed);
|
||||||
|
player = MediaPlayer.create(this, Uri.fromFile(new File(media.getFile_url())));
|
||||||
|
player.setOnPreparedListener(preparedListener);
|
||||||
|
Log.d(TAG, "Preparing to play file");
|
||||||
|
//player.prepareAsync();
|
||||||
|
}
|
||||||
|
return Service.START_NOT_STICKY;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MediaPlayer.OnPreparedListener preparedListener = new MediaPlayer.OnPreparedListener() {
|
||||||
|
@Override
|
||||||
|
public void onPrepared(MediaPlayer mp) {
|
||||||
|
Log.d(TAG, "Resource prepared");
|
||||||
|
mp.start();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue