diff options
author | Tom Hennen <TomHennen@users.noreply.github.com> | 2015-09-17 18:54:21 -0400 |
---|---|---|
committer | Tom Hennen <TomHennen@users.noreply.github.com> | 2015-09-17 18:54:21 -0400 |
commit | 155eeb49b335c134bb971a921ce16a4eff208491 (patch) | |
tree | 1215b80abf2abd50533b4d144e83aaa000df1912 | |
parent | 8d4bdd5ba1a027a468e1a40eadc2b3ae6fc62faf (diff) | |
parent | c34a2779bb0d9a1cd9faa285a1f3f8409255d9c7 (diff) | |
download | AntennaPod-155eeb49b335c134bb971a921ce16a4eff208491.zip |
Merge pull request #1204 from mfietz/media_session
Lockscreen background, media session and playback state
6 files changed, 89 insertions, 16 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 117a008a8..a94f9ff95 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -325,6 +325,11 @@ android:scheme="package"/> </intent-filter> </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 android:name="de.danoeh.antennapod.core.glide.ApGlideModule" diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 35fb60c58..c9be65f2b 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -47,6 +47,12 @@ <CheckBoxPreference android:defaultValue="true" android:enabled="true" + android:key="prefLockscreenBackground" + android:summary="@string/pref_lockscreen_background_sum" + android:title="@string/pref_lockscreen_background_title"/> + <CheckBoxPreference + android:defaultValue="true" + android:enabled="true" android:key="prefShowDownloadReport" android:summary="@string/pref_showDownloadReport_sum" android:title="@string/pref_showDownloadReport_title"/> diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 08328536d..c0d44dff3 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -47,6 +47,7 @@ public class UserPreferences { public static final String PREF_DRAWER_FEED_COUNTER = "prefDrawerFeedIndicator"; public static final String PREF_EXPANDED_NOTIFICATION = "prefExpandNotify"; 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"; // Queue @@ -172,6 +173,15 @@ public class UserPreferences { } /** + * 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, true); + } + + /** * Returns true if download reports are shown * * @return {@code true} if download reports are shown, {@code false} otherwise diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/MediaButtonIntentReceiver.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/MediaButtonIntentReceiver.java index 3e401b5ea..7d06390f2 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/MediaButtonIntentReceiver.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/MediaButtonIntentReceiver.java @@ -4,11 +4,10 @@ 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 final String TAG = "MediaButtonIntentRcver"; private static PlaybackServiceMediaPlayer mMediaPlayer; @@ -20,7 +19,7 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver { 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)); + mMediaPlayer.handleMediaKey(intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java index 2012549fc..757b2e980 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java @@ -4,9 +4,12 @@ import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.graphics.Bitmap; import android.media.AudioManager; import android.net.wifi.WifiManager; import android.os.PowerManager; +import android.preference.PreferenceManager; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; @@ -16,6 +19,9 @@ import android.util.Pair; import android.view.KeyEvent; import android.view.SurfaceHolder; +import com.bumptech.glide.Glide; +import com.bumptech.glide.request.target.Target; + import org.apache.commons.lang3.Validate; 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.FeedMedia; 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.storage.DBWriter; 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. */ -public class PlaybackServiceMediaPlayer { +public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPreferenceChangeListener { public static final String TAG = "PlaybackSvcMediaPlayer"; /** @@ -103,7 +110,7 @@ public class PlaybackServiceMediaPlayer { 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); + PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT); mediaSession = new MediaSessionCompat(context, TAG, eventReceiver, buttonReceiverIntent); mediaSession.setCallback(sessionCallback); @@ -116,6 +123,16 @@ public class PlaybackServiceMediaPlayer { mediaType = MediaType.UNKNOWN; playerStatus = PlayerStatus.STOPPED; 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); try { media.loadMetadata(); - mediaSession.setMetadata(getMediaSessionMetadata(media)); + updateMediaSessionMetadata(); if (stream) { mediaPlayer.setDataSource(media.getStreamUrl()); } else { @@ -255,13 +272,36 @@ 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(); + private void updateMediaSessionMetadata() { + executor.execute(() -> { + final Playable p = this.media; + if(p == null) { + return; + } + MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); + builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, p.getFeedTitle()); + builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, p.getEpisodeTitle()); + 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 +852,12 @@ public class PlaybackServiceMediaPlayer { } else { 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)); } @@ -1218,7 +1263,14 @@ public class PlaybackServiceMediaPlayer { stop(); return true; } + case KeyEvent.KEYCODE_MEDIA_NEXT: + { + Log.d(TAG, "Received next event from RemoteControlClient"); + endPlayback(); + return true; + } default: + Log.d(TAG, "Unhandled key code: " + event.getKeyCode()); break; } } diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 3682d4307..68c5150e5 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -1,8 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" - tools:ignore="MissingTranslation" - > + tools:ignore="MissingTranslation"> <!-- Activitiy and fragment titles --> <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_persistNotify_title">Persistent Playback Controls</string> <string name="pref_persistNotify_sum">Keep notification and lockscreen controls when playback is paused.</string> + <string name="pref_lockscreen_background_title">Set Lockscreen Background</string> + <string name="pref_lockscreen_background_sum">Set the lockscreen background to the current episode\'s image. As a side effect, this will also show the image in third party apps.</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_expand_notify_unsupport_toast">Android versions before 4.1 do not support expanded notifications.</string> |