Set lockscreen background, set media session and playback state

This commit is contained in:
Martin Fietz 2015-09-14 21:49:43 +02:00
parent 8d4bdd5ba1
commit 8efb73a39e
6 changed files with 86 additions and 16 deletions

View File

@ -325,6 +325,11 @@
android:scheme="package"/> android:scheme="package"/>
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name="de.danoeh.antennapod.core.service.playback.MediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<meta-data <meta-data
android:name="de.danoeh.antennapod.core.glide.ApGlideModule" android:name="de.danoeh.antennapod.core.glide.ApGlideModule"

View File

@ -44,6 +44,12 @@
android:key="prefPersistNotify" android:key="prefPersistNotify"
android:summary="@string/pref_persistNotify_sum" android:summary="@string/pref_persistNotify_sum"
android:title="@string/pref_persistNotify_title"/> android:title="@string/pref_persistNotify_title"/>
<CheckBoxPreference
android:defaultValue="false"
android:enabled="true"
android:key="prefLockscreenBackground"
android:summary="@string/pref_lockscreen_background_sum"
android:title="@string/pref_lockscreen_background_title"/>
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="true" android:defaultValue="true"
android:enabled="true" android:enabled="true"

View File

@ -47,6 +47,7 @@ public class UserPreferences {
public static final String PREF_DRAWER_FEED_COUNTER = "prefDrawerFeedIndicator"; public static final String PREF_DRAWER_FEED_COUNTER = "prefDrawerFeedIndicator";
public static final String PREF_EXPANDED_NOTIFICATION = "prefExpandNotify"; public static final String PREF_EXPANDED_NOTIFICATION = "prefExpandNotify";
public static final String PREF_PERSISTENT_NOTIFICATION = "prefPersistNotify"; public static final String PREF_PERSISTENT_NOTIFICATION = "prefPersistNotify";
public static final String PREF_LOCKSCREEN_BACKGROUND = "prefLockscreenBackground";
public static final String PREF_SHOW_DOWNLOAD_REPORT = "prefShowDownloadReport"; public static final String PREF_SHOW_DOWNLOAD_REPORT = "prefShowDownloadReport";
// Queue // Queue
@ -171,6 +172,15 @@ public class UserPreferences {
return prefs.getBoolean(PREF_PERSISTENT_NOTIFICATION, false); return prefs.getBoolean(PREF_PERSISTENT_NOTIFICATION, false);
} }
/**
* Returns true if notifications are persistent
*
* @return {@code true} if notifications are persistent, {@code false} otherwise
*/
public static boolean setLockscreenBackground() {
return prefs.getBoolean(PREF_LOCKSCREEN_BACKGROUND, false);
}
/** /**
* Returns true if download reports are shown * Returns true if download reports are shown
* *

View File

@ -4,11 +4,10 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent;
public class MediaButtonIntentReceiver extends BroadcastReceiver { public class MediaButtonIntentReceiver extends BroadcastReceiver {
private static final String TAG = "MediaButtonIntentReceiver"; private static final String TAG = "MediaButtonIntentRcver";
private static PlaybackServiceMediaPlayer mMediaPlayer; private static PlaybackServiceMediaPlayer mMediaPlayer;
@ -20,7 +19,7 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive(Context, " + intent.toString() +")"); Log.d(TAG, "onReceive(Context, " + intent.toString() +")");
if (mMediaPlayer != null && Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { if (mMediaPlayer != null && Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
mMediaPlayer.handleMediaKey((KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)); mMediaPlayer.handleMediaKey(intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT));
} }
} }

View File

@ -4,9 +4,12 @@ import android.app.PendingIntent;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.PowerManager; import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.media.session.PlaybackStateCompat;
@ -16,6 +19,9 @@ import android.util.Pair;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import java.io.IOException; import java.io.IOException;
@ -30,6 +36,7 @@ import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.playback.AudioPlayer; import de.danoeh.antennapod.core.util.playback.AudioPlayer;
@ -40,7 +47,7 @@ import de.danoeh.antennapod.core.util.playback.VideoPlayer;
/** /**
* Manages the MediaPlayer object of the PlaybackService. * Manages the MediaPlayer object of the PlaybackService.
*/ */
public class PlaybackServiceMediaPlayer { public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String TAG = "PlaybackSvcMediaPlayer"; public static final String TAG = "PlaybackSvcMediaPlayer";
/** /**
@ -103,7 +110,7 @@ public class PlaybackServiceMediaPlayer {
ComponentName eventReceiver = new ComponentName(context.getPackageName(), MediaButtonIntentReceiver.class.getName()); ComponentName eventReceiver = new ComponentName(context.getPackageName(), MediaButtonIntentReceiver.class.getName());
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(eventReceiver); mediaButtonIntent.setComponent(eventReceiver);
PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0); PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSession = new MediaSessionCompat(context, TAG, eventReceiver, buttonReceiverIntent); mediaSession = new MediaSessionCompat(context, TAG, eventReceiver, buttonReceiverIntent);
mediaSession.setCallback(sessionCallback); mediaSession.setCallback(sessionCallback);
@ -116,6 +123,16 @@ public class PlaybackServiceMediaPlayer {
mediaType = MediaType.UNKNOWN; mediaType = MediaType.UNKNOWN;
playerStatus = PlayerStatus.STOPPED; playerStatus = PlayerStatus.STOPPED;
videoSize = null; videoSize = null;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(UserPreferences.PREF_LOCKSCREEN_BACKGROUND)) {
updateMediaSessionMetadata();
}
} }
/** /**
@ -224,7 +241,7 @@ public class PlaybackServiceMediaPlayer {
setPlayerStatus(PlayerStatus.INITIALIZING, media); setPlayerStatus(PlayerStatus.INITIALIZING, media);
try { try {
media.loadMetadata(); media.loadMetadata();
mediaSession.setMetadata(getMediaSessionMetadata(media)); updateMediaSessionMetadata();
if (stream) { if (stream) {
mediaPlayer.setDataSource(media.getStreamUrl()); mediaPlayer.setDataSource(media.getStreamUrl());
} else { } else {
@ -255,13 +272,33 @@ public class PlaybackServiceMediaPlayer {
} }
} }
private MediaMetadataCompat getMediaSessionMetadata(Playable p) { private void updateMediaSessionMetadata() {
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); executor.execute(() -> {
builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, p.getFeedTitle()); Playable p = this.media;
builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, p.getEpisodeTitle()); MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, p.getEpisodeTitle()); builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, p.getFeedTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, p.getFeedTitle()); builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, p.getEpisodeTitle());
return builder.build(); builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, p.getDuration());
builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, p.getEpisodeTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, p.getFeedTitle());
if (p.getImageUri() != null) {
if (UserPreferences.setLockscreenBackground()) {
builder.putString(MediaMetadataCompat.METADATA_KEY_ART_URI, p.getImageUri().toString());
try {
Bitmap art = Glide.with(context)
.load(p.getImageUri())
.asBitmap()
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, art);
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
}
mediaSession.setMetadata(builder.build());
});
} }
@ -812,7 +849,12 @@ public class PlaybackServiceMediaPlayer {
} else { } else {
state = PlaybackStateCompat.STATE_NONE; state = PlaybackStateCompat.STATE_NONE;
} }
sessionState.setState(state, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, getPlaybackSpeed()); sessionState.setState(state, getPosition(), getPlaybackSpeed());
sessionState.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE
| PlaybackStateCompat.ACTION_REWIND
| PlaybackStateCompat.ACTION_FAST_FORWARD
| PlaybackStateCompat.ACTION_SKIP_TO_NEXT);
mediaSession.setPlaybackState(sessionState.build());
callback.statusChanged(new PSMPInfo(playerStatus, media)); callback.statusChanged(new PSMPInfo(playerStatus, media));
} }
@ -1218,7 +1260,14 @@ public class PlaybackServiceMediaPlayer {
stop(); stop();
return true; return true;
} }
case KeyEvent.KEYCODE_MEDIA_NEXT:
{
Log.d(TAG, "Received next event from RemoteControlClient");
endPlayback();
return true;
}
default: default:
Log.d(TAG, "Unhandled key code: " + event.getKeyCode());
break; break;
} }
} }

View File

@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources <resources
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation" tools:ignore="MissingTranslation">
>
<!-- Activitiy and fragment titles --> <!-- Activitiy and fragment titles -->
<string name="app_name">AntennaPod</string> <string name="app_name">AntennaPod</string>
@ -335,6 +334,8 @@
<string name="pref_expandNotify_sum">Always expand the notification to show playback buttons.</string> <string name="pref_expandNotify_sum">Always expand the notification to show playback buttons.</string>
<string name="pref_persistNotify_title">Persistent Playback Controls</string> <string name="pref_persistNotify_title">Persistent Playback Controls</string>
<string name="pref_persistNotify_sum">Keep notification and lockscreen controls when playback is paused.</string> <string name="pref_persistNotify_sum">Keep notification and lockscreen controls when playback is paused.</string>
<string name="pref_lockscreen_background_title">Lockscreen Background</string>
<string name="pref_lockscreen_background_sum">Set the lockscreen background to the current episode\'s image.</string>
<string name="pref_showDownloadReport_title">Show Download Report</string> <string name="pref_showDownloadReport_title">Show Download Report</string>
<string name="pref_showDownloadReport_sum">If downloads fail, generate a report that shows the details of the failure.</string> <string name="pref_showDownloadReport_sum">If downloads fail, generate a report that shows the details of the failure.</string>
<string name="pref_expand_notify_unsupport_toast">Android versions before 4.1 do not support expanded notifications.</string> <string name="pref_expand_notify_unsupport_toast">Android versions before 4.1 do not support expanded notifications.</string>