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());
|
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
|
||||||
}
|
}
|
||||||
Log.d("onMediaSessionMetadata", title + " " + artist + " " + album);
|
Log.d("onMediaSessionMetadata", title + " " + artist + " " + album);
|
||||||
|
mMediaSession.updateMetadata(title, artist, album);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -241,13 +242,16 @@ public class MainActivity extends Activity implements Servo.Client {
|
||||||
if (mMediaSession == null) {
|
if (mMediaSession == null) {
|
||||||
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
|
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mMediaSession.setPlaybackState(state);
|
||||||
|
|
||||||
if (state == MediaSession.PLAYBACK_STATE_NONE) {
|
if (state == MediaSession.PLAYBACK_STATE_NONE) {
|
||||||
mMediaSession.hideMediaSessionControls();
|
mMediaSession.hideMediaSessionControls();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (state == MediaSession.PLAYBACK_STATE_PLAYING ||
|
if (state == MediaSession.PLAYBACK_STATE_PLAYING ||
|
||||||
state == MediaSession.PLAYBACK_STATE_PAUSED) {
|
state == MediaSession.PLAYBACK_STATE_PAUSED) {
|
||||||
mMediaSession.showMediaSessionControls(state);
|
mMediaSession.showMediaSessionControls();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,9 +58,16 @@ public class MediaSession {
|
||||||
ServoView mView;
|
ServoView mView;
|
||||||
MainActivity mActivity;
|
MainActivity mActivity;
|
||||||
Context mContext;
|
Context mContext;
|
||||||
|
|
||||||
NotificationID mNotificationID;
|
NotificationID mNotificationID;
|
||||||
BroadcastReceiver mMediaSessionActionReceiver;
|
BroadcastReceiver mMediaSessionActionReceiver;
|
||||||
|
|
||||||
|
int mPlaybackState = PLAYBACK_STATE_PAUSED;
|
||||||
|
|
||||||
|
String mTitle;
|
||||||
|
String mArtist;
|
||||||
|
String mAlbum;
|
||||||
|
|
||||||
public MediaSession(ServoView view, MainActivity activity, Context context) {
|
public MediaSession(ServoView view, MainActivity activity, Context context) {
|
||||||
mView = view;
|
mView = view;
|
||||||
mActivity = activity;
|
mActivity = activity;
|
||||||
|
@ -85,13 +92,13 @@ public class MediaSession {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showMediaSessionControls(int playbackState) {
|
public void showMediaSessionControls() {
|
||||||
Log.d("MediaSession", "showMediaSessionControls " + playbackState);
|
Log.d("MediaSession", "showMediaSessionControls " + mPlaybackState);
|
||||||
IntentFilter filter = new IntentFilter();
|
IntentFilter filter = new IntentFilter();
|
||||||
if (playbackState == PLAYBACK_STATE_PAUSED) {
|
if (mPlaybackState == PLAYBACK_STATE_PAUSED) {
|
||||||
filter.addAction(KEY_MEDIA_PLAY);
|
filter.addAction(KEY_MEDIA_PLAY);
|
||||||
}
|
}
|
||||||
if (playbackState == PLAYBACK_STATE_PLAYING) {
|
if (mPlaybackState == PLAYBACK_STATE_PLAYING) {
|
||||||
filter.addAction(KEY_MEDIA_PAUSE);
|
filter.addAction(KEY_MEDIA_PAUSE);
|
||||||
}
|
}
|
||||||
filter.addAction(KEY_MEDIA_STOP);
|
filter.addAction(KEY_MEDIA_STOP);
|
||||||
|
@ -129,12 +136,28 @@ public class MediaSession {
|
||||||
Notification.Builder builder = new Notification.Builder(mContext, this.MEDIA_CHANNEL_ID);
|
Notification.Builder builder = new Notification.Builder(mContext, this.MEDIA_CHANNEL_ID);
|
||||||
builder
|
builder
|
||||||
.setSmallIcon(R.drawable.media_session_icon)
|
.setSmallIcon(R.drawable.media_session_icon)
|
||||||
.setContentTitle("This is the notification title")
|
.setContentTitle(mTitle)
|
||||||
.setVisibility(Notification.VISIBILITY_PUBLIC)
|
.setVisibility(Notification.VISIBILITY_PUBLIC)
|
||||||
.addAction(stopAction)
|
.addAction(stopAction)
|
||||||
.setStyle(new Notification.MediaStyle());
|
.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);
|
Intent playIntent = new Intent(KEY_MEDIA_PLAY);
|
||||||
Notification.Action playAction =
|
Notification.Action playAction =
|
||||||
new Notification.Action(R.drawable.media_session_play, "Play",
|
new Notification.Action(R.drawable.media_session_play, "Play",
|
||||||
|
@ -142,7 +165,7 @@ public class MediaSession {
|
||||||
builder.addAction(playAction);
|
builder.addAction(playAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playbackState == PLAYBACK_STATE_PLAYING) {
|
if (mPlaybackState == PLAYBACK_STATE_PLAYING) {
|
||||||
Intent pauseIntent = new Intent(KEY_MEDIA_PAUSE);
|
Intent pauseIntent = new Intent(KEY_MEDIA_PAUSE);
|
||||||
Notification.Action pauseAction =
|
Notification.Action pauseAction =
|
||||||
new Notification.Action(R.drawable.media_session_pause, "Pause",
|
new Notification.Action(R.drawable.media_session_pause, "Pause",
|
||||||
|
@ -163,4 +186,18 @@ public class MediaSession {
|
||||||
mContext.unregisterReceiver(mMediaSessionActionReceiver);
|
mContext.unregisterReceiver(mMediaSessionActionReceiver);
|
||||||
mMediaSessionActionReceiver = null;
|
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