Merge pull request #693 from volhol/develop

Addresses #669 and #664: play buttons in the lists
This commit is contained in:
Tom Hennen 2015-04-03 12:24:04 -04:00
commit 13012a2c41
14 changed files with 157 additions and 7 deletions

View File

@ -57,7 +57,7 @@ public class ActionButtonUtils {
} else {
// item is not being downloaded
butSecondary.setVisibility(View.VISIBLE);
if (media.isPlaying()) {
if (media.isCurrentlyPlaying()) {
butSecondary.setImageDrawable(drawables.getDrawable(3));
} else {
butSecondary

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.adapter;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import org.apache.commons.lang3.Validate;
@ -9,6 +10,7 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.DownloadRequestException;
@ -46,7 +48,15 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
DownloadRequester.getInstance().cancelDownload(context, media);
Toast.makeText(context, R.string.download_cancelled_msg, Toast.LENGTH_SHORT).show();
} else { // media is downloaded
DBTasks.playMedia(context, media, true, true, false);
if (item.hasMedia() && item.getMedia().isCurrentlyPlaying()) {
context.sendBroadcast(new Intent(PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE));
}
else if (item.hasMedia() && item.getMedia().isCurrentlyPaused()) {
context.sendBroadcast(new Intent(PlaybackService.ACTION_RESUME_PLAY_CURRENT_EPISODE));
}
else {
DBTasks.playMedia(context, media, false, true, false);
}
}
} else {
if (!item.isRead()) {

View File

@ -9,6 +9,7 @@ import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;

View File

@ -14,6 +14,7 @@ import android.widget.TextView;
import com.squareup.picasso.Picasso;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.storage.DownloadRequester;

View File

@ -14,6 +14,7 @@ import android.widget.TextView;
import com.squareup.picasso.Picasso;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.storage.DownloadRequester;

View File

@ -66,7 +66,8 @@ public class ItemlistFragment extends ListFragment {
private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED
| EventDistributor.DOWNLOAD_QUEUED
| EventDistributor.QUEUE_UPDATE
| EventDistributor.UNREAD_ITEMS_UPDATE;
| EventDistributor.UNREAD_ITEMS_UPDATE
| EventDistributor.PLAYER_STATUS_UPDATE;
public static final String EXTRA_SELECTED_FEEDITEM = "extra.de.danoeh.antennapod.activity.selected_feeditem";
public static final String ARGUMENT_FEED_ID = "argument.de.danoeh.antennapod.feed_id";

View File

@ -54,7 +54,8 @@ public class NewEpisodesFragment extends Fragment {
private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED |
EventDistributor.DOWNLOAD_QUEUED |
EventDistributor.QUEUE_UPDATE |
EventDistributor.UNREAD_ITEMS_UPDATE;
EventDistributor.UNREAD_ITEMS_UPDATE |
EventDistributor.PLAYER_STATUS_UPDATE;
private static final int RECENT_EPISODES_LIMIT = 150;
private static final String PREF_NAME = "PrefNewEpisodesFragment";

View File

@ -34,6 +34,8 @@ import de.danoeh.antennapod.menuhandler.NavDrawerActivity;
public class PlaybackHistoryFragment extends ListFragment {
private static final String TAG = "PlaybackHistoryFragment";
private static final int EVENTS = EventDistributor.PLAYBACK_HISTORY_UPDATE |
EventDistributor.PLAYER_STATUS_UPDATE;
private List<FeedItem> playbackHistory;
private QueueAccess queue;
@ -167,7 +169,7 @@ public class PlaybackHistoryFragment extends ListFragment {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((arg & EventDistributor.PLAYBACK_HISTORY_UPDATE) != 0) {
if ((arg & EVENTS) != 0) {
startItemLoader();
getActivity().supportInvalidateOptionsMenu();
}

View File

@ -56,7 +56,8 @@ public class QueueFragment extends Fragment {
private static final String TAG = "QueueFragment";
private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED |
EventDistributor.DOWNLOAD_QUEUED |
EventDistributor.QUEUE_UPDATE;
EventDistributor.QUEUE_UPDATE |
EventDistributor.PLAYER_STATUS_UPDATE;
private DragSortListView listView;
private QueueListAdapter listAdapter;

View File

@ -31,6 +31,7 @@ public class EventDistributor extends Observable {
public static final int PLAYBACK_HISTORY_UPDATE = 16;
public static final int DOWNLOAD_QUEUED = 32;
public static final int DOWNLOAD_HANDLED = 64;
public static final int PLAYER_STATUS_UPDATE = 128;
private Handler handler;
private AbstractQueue<Integer> events;
@ -124,6 +125,10 @@ public class EventDistributor extends Observable {
addEvent(DOWNLOAD_HANDLED);
}
public void sendPlayerStatusUpdateBroadcast() {
addEvent(PLAYER_STATUS_UPDATE);
}
public static abstract class EventListener implements Observer {
@Override

View File

@ -127,6 +127,25 @@ public class FeedMedia extends FeedFile implements Playable {
&& PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == id;
}
/**
* Reads playback preferences to determine whether this FeedMedia object is
* currently being played and the current player status is playing.
*/
public boolean isCurrentlyPlaying() {
return isPlaying() &&
((PlaybackPreferences.getCurrentPlayerStatus() == PlaybackPreferences.PLAYER_STATUS_PLAYING));
}
/**
* Reads playback preferences to determine whether this FeedMedia object is
* currently being played and the current player status is paused.
*/
public boolean isCurrentlyPaused() {
return isPlaying() &&
((PlaybackPreferences.getCurrentPlayerStatus() == PlaybackPreferences.PLAYER_STATUS_PAUSED));
}
@Override
public int getTypeAsInt() {
return FEEDFILETYPE_FEEDMEDIA;

View File

@ -8,6 +8,7 @@ import android.util.Log;
import org.apache.commons.lang3.Validate;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.EventDistributor;
/**
* Provides access to preferences set by the playback service. A private
@ -43,14 +44,27 @@ public class PlaybackPreferences implements
/** True if last played media was a video. */
public static final String PREF_CURRENT_EPISODE_IS_VIDEO = "de.danoeh.antennapod.preferences.lastIsVideo";
/** The current player status as int. */
public static final String PREF_CURRENT_PLAYER_STATUS = "de.danoeh.antennapod.preferences.currentPlayerStatus";
/** Value of PREF_CURRENTLY_PLAYING_MEDIA if no media is playing. */
public static final long NO_MEDIA_PLAYING = -1;
/** Value of PREF_CURRENT_PLAYER_STATUS if media player status is playing. */
public static final int PLAYER_STATUS_PLAYING = 1;
/** Value of PREF_CURRENT_PLAYER_STATUS if media player status is paused. */
public static final int PLAYER_STATUS_PAUSED = 2;
/** Value of PREF_CURRENT_PLAYER_STATUS if media player status is neither playing nor paused. */
public static final int PLAYER_STATUS_OTHER = 3;
private long currentlyPlayingFeedId;
private long currentlyPlayingFeedMediaId;
private long currentlyPlayingMedia;
private boolean currentEpisodeIsStream;
private boolean currentEpisodeIsVideo;
private int currentPlayerStatus;
private static PlaybackPreferences instance;
private Context context;
@ -87,6 +101,8 @@ public class PlaybackPreferences implements
NO_MEDIA_PLAYING);
currentEpisodeIsStream = sp.getBoolean(PREF_CURRENT_EPISODE_IS_STREAM, true);
currentEpisodeIsVideo = sp.getBoolean(PREF_CURRENT_EPISODE_IS_VIDEO, false);
currentPlayerStatus = sp.getInt(PREF_CURRENT_PLAYER_STATUS,
PLAYER_STATUS_OTHER);
}
@Override
@ -109,6 +125,11 @@ public class PlaybackPreferences implements
currentlyPlayingFeedMediaId = sp.getLong(
PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID, NO_MEDIA_PLAYING);
}
else if (key.equals(PREF_CURRENT_PLAYER_STATUS)) {
currentPlayerStatus = sp.getInt(PREF_CURRENT_PLAYER_STATUS,
PLAYER_STATUS_OTHER);
EventDistributor.getInstance().sendPlayerStatusUpdateBroadcast();
}
}
private static void instanceAvailable() {
@ -143,4 +164,10 @@ public class PlaybackPreferences implements
return instance.currentEpisodeIsVideo;
}
public static int getCurrentPlayerStatus() {
instanceAvailable();
return instance.currentPlayerStatus;
}
}

View File

@ -100,6 +100,18 @@ public class PlaybackService extends Service {
*/
public static final String ACTION_SKIP_CURRENT_EPISODE = "action.de.danoeh.antennapod.core.service.skipCurrentEpisode";
/**
* If the PlaybackService receives this action, it will pause playback.
*/
public static final String ACTION_PAUSE_PLAY_CURRENT_EPISODE = "action.de.danoeh.antennapod.core.service.pausePlayCurrentEpisode";
/**
* If the PlaybackService receives this action, it will resume playback.
*/
public static final String ACTION_RESUME_PLAY_CURRENT_EPISODE = "action.de.danoeh.antennapod.core.service.resumePlayCurrentEpisode";
/**
* Used in NOTIFICATION_TYPE_RELOAD.
*/
@ -216,6 +228,10 @@ public class PlaybackService extends Service {
AudioManager.ACTION_AUDIO_BECOMING_NOISY));
registerReceiver(skipCurrentEpisodeReceiver, new IntentFilter(
ACTION_SKIP_CURRENT_EPISODE));
registerReceiver(pausePlayCurrentEpisodeReceiver, new IntentFilter(
ACTION_PAUSE_PLAY_CURRENT_EPISODE));
registerReceiver(pauseResumeCurrentEpisodeReceiver, new IntentFilter(
ACTION_RESUME_PLAY_CURRENT_EPISODE));
remoteControlClient = setupRemoteControlClient();
taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback);
mediaPlayer = new PlaybackServiceMediaPlayer(this, mediaPlayerCallback);
@ -427,6 +443,7 @@ public class PlaybackService extends Service {
// remove notifcation on pause
stopForeground(true);
}
writePlayerStatusPlaybackPreferences();
break;
@ -443,9 +460,11 @@ public class PlaybackService extends Service {
taskManager.startPositionSaver();
taskManager.startWidgetUpdater();
writePlayerStatusPlaybackPreferences();
setupNotification(newInfo);
started = true;
break;
case ERROR:
writePlaybackPreferencesNoMediaPlaying();
break;
@ -634,9 +653,26 @@ public class PlaybackService extends Service {
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.putInt(
PlaybackPreferences.PREF_CURRENT_PLAYER_STATUS,
PlaybackPreferences.PLAYER_STATUS_OTHER);
editor.commit();
}
private int getCurrentPlayerStatusAsInt(PlayerStatus playerStatus) {
int playerStatusAsInt;
switch (playerStatus) {
case PLAYING:
playerStatusAsInt = PlaybackPreferences.PLAYER_STATUS_PLAYING;
break;
case PAUSED:
playerStatusAsInt = PlaybackPreferences.PLAYER_STATUS_PAUSED;
break;
default:
playerStatusAsInt = PlaybackPreferences.PLAYER_STATUS_OTHER;
}
return playerStatusAsInt;
}
private void writePlaybackPreferences() {
if (BuildConfig.DEBUG)
@ -647,6 +683,7 @@ public class PlaybackService extends Service {
PlaybackServiceMediaPlayer.PSMPInfo info = mediaPlayer.getPSMPInfo();
MediaType mediaType = mediaPlayer.getCurrentMediaType();
boolean stream = mediaPlayer.isStreaming();
int playerStatus = getCurrentPlayerStatusAsInt(info.playerStatus);
if (info.playable != null) {
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_MEDIA,
@ -683,6 +720,23 @@ public class PlaybackService extends Service {
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
}
editor.putInt(
PlaybackPreferences.PREF_CURRENT_PLAYER_STATUS, playerStatus);
editor.commit();
}
private void writePlayerStatusPlaybackPreferences() {
if (BuildConfig.DEBUG)
Log.d(TAG, "Writing player status playback preferences");
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()).edit();
PlaybackServiceMediaPlayer.PSMPInfo info = mediaPlayer.getPSMPInfo();
int playerStatus = getCurrentPlayerStatusAsInt(info.playerStatus);
editor.putInt(
PlaybackPreferences.PREF_CURRENT_PLAYER_STATUS, playerStatus);
editor.commit();
}
@ -1101,6 +1155,28 @@ public class PlaybackService extends Service {
}
};
private BroadcastReceiver pauseResumeCurrentEpisodeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (StringUtils.equals(intent.getAction(), ACTION_RESUME_PLAY_CURRENT_EPISODE)) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Received RESUME_PLAY_CURRENT_EPISODE intent");
mediaPlayer.resume();
}
}
};
private BroadcastReceiver pausePlayCurrentEpisodeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (StringUtils.equals(intent.getAction(), ACTION_PAUSE_PLAY_CURRENT_EPISODE)) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Received PAUSE_PLAY_CURRENT_EPISODE intent");
mediaPlayer.pause(false, false);
}
}
};
public static MediaType getCurrentMediaType() {
return currentMediaType;
}

View File

@ -169,7 +169,8 @@ public class PlaybackServiceMediaPlayer {
if (media != null) {
if (!forceReset && media.getIdentifier().equals(playable.getIdentifier())) {
if (!forceReset && media.getIdentifier().equals(playable.getIdentifier())
&& playerStatus == PlayerStatus.PLAYING) {
// episode is already playing -> ignore method call
if (BuildConfig.DEBUG)
Log.d(TAG, "Method call to playMediaObject was ignored: media file already playing.");
@ -179,6 +180,10 @@ public class PlaybackServiceMediaPlayer {
if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PLAYING || playerStatus == PlayerStatus.PREPARED) {
mediaPlayer.stop();
}
// set temporarily to pause in order to update list with current position
if (playerStatus == PlayerStatus.PLAYING) {
setPlayerStatus(PlayerStatus.PAUSED, media);
}
setPlayerStatus(PlayerStatus.INDETERMINATE, null);
}
}