mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Update media session metadata and show content text with artist and album
This commit is contained in:
parent
d33c96b31e
commit
f65c400828
2 changed files with 49 additions and 8 deletions
|
@ -233,6 +233,7 @@ public class MainActivity extends Activity implements Servo.Client {
|
|||
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
|
||||
}
|
||||
Log.d("onMediaSessionMetadata", title + " " + artist + " " + album);
|
||||
mMediaSession.updateMetadata(title, artist, album);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -241,13 +242,16 @@ public class MainActivity extends Activity implements Servo.Client {
|
|||
if (mMediaSession == null) {
|
||||
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
|
||||
}
|
||||
|
||||
mMediaSession.setPlaybackState(state);
|
||||
|
||||
if (state == MediaSession.PLAYBACK_STATE_NONE) {
|
||||
mMediaSession.hideMediaSessionControls();
|
||||
return;
|
||||
}
|
||||
if (state == MediaSession.PLAYBACK_STATE_PLAYING ||
|
||||
state == MediaSession.PLAYBACK_STATE_PAUSED) {
|
||||
mMediaSession.showMediaSessionControls(state);
|
||||
mMediaSession.showMediaSessionControls();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,9 +58,16 @@ public class MediaSession {
|
|||
ServoView mView;
|
||||
MainActivity mActivity;
|
||||
Context mContext;
|
||||
|
||||
NotificationID mNotificationID;
|
||||
BroadcastReceiver mMediaSessionActionReceiver;
|
||||
|
||||
int mPlaybackState = PLAYBACK_STATE_PAUSED;
|
||||
|
||||
String mTitle;
|
||||
String mArtist;
|
||||
String mAlbum;
|
||||
|
||||
public MediaSession(ServoView view, MainActivity activity, Context context) {
|
||||
mView = view;
|
||||
mActivity = activity;
|
||||
|
@ -85,13 +92,13 @@ public class MediaSession {
|
|||
}
|
||||
}
|
||||
|
||||
public void showMediaSessionControls(int playbackState) {
|
||||
Log.d("MediaSession", "showMediaSessionControls " + playbackState);
|
||||
public void showMediaSessionControls() {
|
||||
Log.d("MediaSession", "showMediaSessionControls " + mPlaybackState);
|
||||
IntentFilter filter = new IntentFilter();
|
||||
if (playbackState == PLAYBACK_STATE_PAUSED) {
|
||||
if (mPlaybackState == PLAYBACK_STATE_PAUSED) {
|
||||
filter.addAction(KEY_MEDIA_PLAY);
|
||||
}
|
||||
if (playbackState == PLAYBACK_STATE_PLAYING) {
|
||||
if (mPlaybackState == PLAYBACK_STATE_PLAYING) {
|
||||
filter.addAction(KEY_MEDIA_PAUSE);
|
||||
}
|
||||
filter.addAction(KEY_MEDIA_STOP);
|
||||
|
@ -129,12 +136,28 @@ public class MediaSession {
|
|||
Notification.Builder builder = new Notification.Builder(mContext, this.MEDIA_CHANNEL_ID);
|
||||
builder
|
||||
.setSmallIcon(R.drawable.media_session_icon)
|
||||
.setContentTitle("This is the notification title")
|
||||
.setContentTitle(mTitle)
|
||||
.setVisibility(Notification.VISIBILITY_PUBLIC)
|
||||
.addAction(stopAction)
|
||||
.setStyle(new Notification.MediaStyle());
|
||||
|
||||
if (playbackState == PLAYBACK_STATE_PAUSED) {
|
||||
String contentText = new String();
|
||||
if (mArtist != null && !mArtist.isEmpty()) {
|
||||
contentText = mArtist;
|
||||
}
|
||||
if (mAlbum != null && !mAlbum.isEmpty()) {
|
||||
if (!contentText.isEmpty()) {
|
||||
contentText += " - " + mAlbum;
|
||||
} else {
|
||||
contentText = mAlbum;
|
||||
}
|
||||
}
|
||||
|
||||
if (!contentText.isEmpty()) {
|
||||
builder.setContentText(contentText);
|
||||
}
|
||||
|
||||
if (mPlaybackState == PLAYBACK_STATE_PAUSED) {
|
||||
Intent playIntent = new Intent(KEY_MEDIA_PLAY);
|
||||
Notification.Action playAction =
|
||||
new Notification.Action(R.drawable.media_session_play, "Play",
|
||||
|
@ -142,7 +165,7 @@ public class MediaSession {
|
|||
builder.addAction(playAction);
|
||||
}
|
||||
|
||||
if (playbackState == PLAYBACK_STATE_PLAYING) {
|
||||
if (mPlaybackState == PLAYBACK_STATE_PLAYING) {
|
||||
Intent pauseIntent = new Intent(KEY_MEDIA_PAUSE);
|
||||
Notification.Action pauseAction =
|
||||
new Notification.Action(R.drawable.media_session_pause, "Pause",
|
||||
|
@ -163,4 +186,18 @@ public class MediaSession {
|
|||
mContext.unregisterReceiver(mMediaSessionActionReceiver);
|
||||
mMediaSessionActionReceiver = null;
|
||||
}
|
||||
|
||||
public void setPlaybackState(int state) {
|
||||
mPlaybackState = state;
|
||||
}
|
||||
|
||||
public void updateMetadata(String title, String artist, String album) {
|
||||
mTitle = title;
|
||||
mArtist = artist;
|
||||
mAlbum = album;
|
||||
|
||||
if (mMediaSessionActionReceiver != null) {
|
||||
showMediaSessionControls();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue