Don't check if service is running when starting a podcast

Add base class MediaItem to PodcastItem and TTSItem.

If the service is already running, onStartCommand will be called again
when startService is called and the new MediaItem will be started.

Fixes a NPE when a podcast is started when the service is already
running.
This commit is contained in:
Daniel Schaal 2015-07-29 06:12:59 +02:00
parent 8387d057a1
commit de94c70b1e
5 changed files with 26 additions and 39 deletions

View file

@ -448,7 +448,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
case R.id.action_tts: case R.id.action_tts:
TTSItem ttsItem = new TTSItem(rssItem.getId(), rssItem.getTitle(), rssItem.getTitle() + "\n\n " + Html.fromHtml(rssItem.getBody()).toString(), rssItem.getFeed().getFaviconUrl()); TTSItem ttsItem = new TTSItem(rssItem.getId(), rssItem.getTitle(), rssItem.getTitle() + "\n\n " + Html.fromHtml(rssItem.getBody()).toString(), rssItem.getFeed().getFaviconUrl());
openTTSItem(ttsItem); openMediaItem(ttsItem);
break; break;
case R.id.action_ShareItem: case R.id.action_ShareItem:

View file

@ -39,6 +39,7 @@ import de.luhmer.owncloudnewsreader.events.podcast.UpdatePodcastStatusEvent;
import de.luhmer.owncloudnewsreader.events.podcast.VideoDoubleClicked; import de.luhmer.owncloudnewsreader.events.podcast.VideoDoubleClicked;
import de.luhmer.owncloudnewsreader.helper.SizeAnimator; import de.luhmer.owncloudnewsreader.helper.SizeAnimator;
import de.luhmer.owncloudnewsreader.interfaces.IPlayPausePodcastClicked; import de.luhmer.owncloudnewsreader.interfaces.IPlayPausePodcastClicked;
import de.luhmer.owncloudnewsreader.model.MediaItem;
import de.luhmer.owncloudnewsreader.model.PodcastItem; import de.luhmer.owncloudnewsreader.model.PodcastItem;
import de.luhmer.owncloudnewsreader.model.TTSItem; import de.luhmer.owncloudnewsreader.model.TTSItem;
import de.luhmer.owncloudnewsreader.services.PodcastDownloadService; import de.luhmer.owncloudnewsreader.services.PodcastDownloadService;
@ -55,7 +56,7 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
boolean mBound = false; boolean mBound = false;
private static final String TAG = "PodcastSherlockFragmentActivity"; private static final String TAG = "PodcastFragmentActivity";
private PodcastFragment mPodcastFragment; private PodcastFragment mPodcastFragment;
private EventBus eventBus; private EventBus eventBus;
@ -501,30 +502,13 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
return px; return px;
} }
private void openPodcast(PodcastItem podcastItem) { protected void openMediaItem(MediaItem mediaItem) {
// Bind to LocalService
Intent intent = new Intent(this, PodcastPlaybackService.class); Intent intent = new Intent(this, PodcastPlaybackService.class);
if(!isMyServiceRunning(PodcastPlaybackService.class)) { if(mediaItem instanceof TTSItem)
intent.putExtra(PodcastPlaybackService.PODCAST_ITEM, podcastItem); intent.putExtra(PodcastPlaybackService.TTS_ITEM, mediaItem);
startService(intent); else
} else { intent.putExtra(PodcastPlaybackService.PODCAST_ITEM, mediaItem);
mPodcastPlaybackService.openFile(podcastItem); startService(intent);
loadPodcastFavIcon();
}
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
protected void openTTSItem(TTSItem ttsItem) {
// Bind to LocalService
Intent intent = new Intent(this, PodcastPlaybackService.class);
if(!isMyServiceRunning(PodcastPlaybackService.class)) {
intent.putExtra(PodcastPlaybackService.TTS_ITEM, ttsItem);
startService(intent);
} else {
mPodcastPlaybackService.openTtsFeed(ttsItem);
loadPodcastFavIcon();
}
bindService(intent, mConnection, Context.BIND_AUTO_CREATE); bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
} }
@ -537,7 +521,7 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
if(file.exists()) { if(file.exists()) {
podcastItem.link = file.getAbsolutePath(); podcastItem.link = file.getAbsolutePath();
openPodcast(podcastItem); openMediaItem(podcastItem);
} else if(!podcastItem.offlineCached) { } else if(!podcastItem.offlineCached) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
@ -558,7 +542,7 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
alertDialog.setPositiveButton("Stream", new DialogInterface.OnClickListener() { alertDialog.setPositiveButton("Stream", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
openPodcast(podcastItem); openMediaItem(podcastItem);
} }
}); });
} }

View file

@ -0,0 +1,13 @@
package de.luhmer.owncloudnewsreader.model;
import java.io.Serializable;
/**
* Created by daniel on 29.07.15.
*/
public abstract class MediaItem implements Serializable {
public long itemId;
public String title;
public String favIcon;
public String link;
}

View file

@ -1,11 +1,9 @@
package de.luhmer.owncloudnewsreader.model; package de.luhmer.owncloudnewsreader.model;
import java.io.Serializable;
/** /**
* Created by David on 21.06.2014. * Created by David on 21.06.2014.
*/ */
public class PodcastItem implements Serializable { public class PodcastItem extends MediaItem {
public PodcastItem() { public PodcastItem() {
@ -21,12 +19,8 @@ public class PodcastItem implements Serializable {
this.isVideoPodcast = isVideoPodcast; this.isVideoPodcast = isVideoPodcast;
} }
public long itemId;
public String title;
public String link;
public String mimeType; public String mimeType;
public boolean offlineCached; public boolean offlineCached;
public String favIcon;
public boolean isVideoPodcast; public boolean isVideoPodcast;
public Integer downloadProgress; public Integer downloadProgress;

View file

@ -5,7 +5,7 @@ import java.io.Serializable;
/** /**
* Created by David on 10.01.2015. * Created by David on 10.01.2015.
*/ */
public class TTSItem implements Serializable { public class TTSItem extends MediaItem {
public TTSItem() { public TTSItem() {
@ -18,9 +18,5 @@ public class TTSItem implements Serializable {
this.favIcon = favIcon; this.favIcon = favIcon;
} }
public long itemId;
public String title;
public String link;
public String text; public String text;
public String favIcon;
} }