Compiling with api 22 with thanks to @mfietz

4ccefe5e84
This commit is contained in:
Tom Hennen 2015-09-03 17:41:19 -04:00
parent e24d4efe10
commit a2e61bee11
3 changed files with 112 additions and 91 deletions

View File

@ -0,0 +1,27 @@
package de.danoeh.antennapod.core.service.playback;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
public class MediaButtonIntentReceiver extends BroadcastReceiver {
private static final String TAG = "MediaButtonIntentReceiver";
private static PlaybackServiceMediaPlayer mMediaPlayer;
public static void setMediaPlayer(PlaybackServiceMediaPlayer mediaPlayer) {
mMediaPlayer = mediaPlayer;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive(Context, " + intent.toString() +")");
if (mMediaPlayer != null && Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
mMediaPlayer.handleMediaKey((KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT));
}
}
}

View File

@ -241,7 +241,6 @@ public class PlaybackService extends Service {
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);
@ -493,7 +492,6 @@ public class PlaybackService extends Service {
// statusUpdate.putExtra(EXTRA_NEW_PLAYER_STATUS, newInfo.playerStatus.ordinal());
sendBroadcast(statusUpdate);
updateWidget();
refreshRemoteControlClientState(newInfo);
bluetoothNotifyChange(newInfo, AVRCP_ACTION_PLAYER_STATUS_CHANGED);
bluetoothNotifyChange(newInfo, AVRCP_ACTION_META_CHANGED);
}
@ -546,11 +544,6 @@ public class PlaybackService extends Service {
PlaybackService.this.endPlayback(true);
return true;
}
@Override
public RemoteControlClient getRemoteControlClient() {
return remoteControlClient;
}
};
private void endPlayback(boolean playNextEpisode) {
@ -960,74 +953,6 @@ public class PlaybackService extends Service {
return taskManager.getSleepTimerTimeLeft();
}
@SuppressLint("NewApi")
private RemoteControlClient setupRemoteControlClient() {
if (Build.VERSION.SDK_INT < 14) {
return null;
}
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(new ComponentName(getPackageName(),
MediaButtonReceiver.class.getName()));
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, mediaButtonIntent, 0);
remoteControlClient = new RemoteControlClient(mediaPendingIntent);
int controlFlags;
if (android.os.Build.VERSION.SDK_INT < 16) {
controlFlags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT;
} else {
controlFlags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE;
}
remoteControlClient.setTransportControlFlags(controlFlags);
return remoteControlClient;
}
/**
* Refresh player status and metadata.
*/
@SuppressLint("NewApi")
private void refreshRemoteControlClientState(PlaybackServiceMediaPlayer.PSMPInfo info) {
if (android.os.Build.VERSION.SDK_INT >= 14) {
if (remoteControlClient != null) {
switch (info.playerStatus) {
case PLAYING:
remoteControlClient
.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
break;
case PAUSED:
case INITIALIZED:
remoteControlClient
.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
break;
case STOPPED:
remoteControlClient
.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
break;
case ERROR:
remoteControlClient
.setPlaybackState(RemoteControlClient.PLAYSTATE_ERROR);
break;
default:
remoteControlClient
.setPlaybackState(RemoteControlClient.PLAYSTATE_BUFFERING);
}
if (info.playable != null) {
MetadataEditor editor = remoteControlClient
.editMetadata(false);
editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE,
info.playable.getEpisodeTitle());
editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM,
info.playable.getFeedTitle());
editor.apply();
}
Log.d(TAG, "RemoteControlClient state was refreshed");
}
}
}
private void bluetoothNotifyChange(PlaybackServiceMediaPlayer.PSMPInfo info, String whatChanged) {
boolean isPlaying = false;

View File

@ -1,9 +1,10 @@
package de.danoeh.antennapod.core.service.playback;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.RemoteControlClient;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.support.v4.media.MediaMetadataCompat;
@ -12,6 +13,7 @@ import android.support.v4.media.session.PlaybackStateCompat;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import org.apache.commons.lang3.Validate;
@ -29,7 +31,6 @@ import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.playback.AudioPlayer;
import de.danoeh.antennapod.core.util.playback.IPlayer;
@ -98,9 +99,16 @@ public class PlaybackServiceMediaPlayer {
}
);
mediaSession = new MediaSessionCompat(context, TAG);
MediaButtonIntentReceiver.setMediaPlayer(this);
ComponentName eventReceiver = new ComponentName(context.getPackageName(), MediaButtonIntentReceiver.class.getName());
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(eventReceiver);
PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);
mediaSession = new MediaSessionCompat(context, TAG, eventReceiver, buttonReceiverIntent);
mediaSession.setCallback(sessionCallback);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
mediaPlayer = null;
statusBeforeSeeking = null;
@ -249,7 +257,9 @@ public class PlaybackServiceMediaPlayer {
private MediaMetadataCompat getMediaSessionMetadata(Playable p) {
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, p.getFeedTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, p.getEpisodeTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, p.getEpisodeTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, p.getFeedTitle());
return builder.build();
}
@ -287,16 +297,6 @@ public class PlaybackServiceMediaPlayer {
setPlayerStatus(PlayerStatus.PLAYING, media);
pausedBecauseOfTransientAudiofocusLoss = false;
if (android.os.Build.VERSION.SDK_INT >= 14) {
RemoteControlClient remoteControlClient = callback.getRemoteControlClient();
if (remoteControlClient != null) {
audioManager
.registerRemoteControlClient(remoteControlClient);
}
}
audioManager
.registerMediaButtonEventReceiver(new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName()));
media.onPlaybackStart();
} else {
@ -949,8 +949,6 @@ public class PlaybackServiceMediaPlayer {
public boolean onMediaPlayerError(Object inObj, int what, int extra);
public boolean endPlayback(boolean playNextEpisode);
public RemoteControlClient getRemoteControlClient();
}
private IPlayer setMediaPlayerListeners(IPlayer mp) {
@ -1128,5 +1126,76 @@ public class PlaybackServiceMediaPlayer {
super.onSeekTo(pos);
seekTo((int) pos);
}
@Override
public boolean onMediaButtonEvent(final Intent mediaButton) {
Log.d(TAG, "GOT MediaButton EVENT");
if (mediaButton != null) {
KeyEvent keyEvent = (KeyEvent) mediaButton.getExtras().get(Intent.EXTRA_KEY_EVENT);
handleMediaKey(keyEvent);
}
return super.onMediaButtonEvent(mediaButton);
}
};
public boolean handleMediaKey(KeyEvent event) {
if (event != null
&& event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_HEADSETHOOK:
{
Log.d(TAG, "Received Play/Pause event from RemoteControlClient");
if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) {
resume();
} else if (playerStatus == PlayerStatus.INITIALIZED) {
setStartWhenPrepared(true);
prepare();
} else if (playerStatus == PlayerStatus.PLAYING) {
pause(false, true);
if (UserPreferences.isPersistNotify()) {
pause(false, true);
} else {
pause(true, true);
}
}
return true;
}
case KeyEvent.KEYCODE_MEDIA_PLAY:
{
Log.d(TAG, "Received Play event from RemoteControlClient");
if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) {
resume();
} else if (playerStatus == PlayerStatus.INITIALIZED) {
setStartWhenPrepared(true);
prepare();
}
return true;
}
case KeyEvent.KEYCODE_MEDIA_PAUSE:
{
Log.d(TAG, "Received Pause event from RemoteControlClient");
if (playerStatus == PlayerStatus.PLAYING) {
pause(false, true);
}
if (UserPreferences.isPersistNotify()) {
pause(false, true);
} else {
pause(true, true);
}
return true;
}
case KeyEvent.KEYCODE_MEDIA_STOP:
{
Log.d(TAG, "Received Stop event from RemoteControlClient");
stop();
return true;
}
default:
break;
}
}
return false;
}
}