Commit ac0456f4 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Limit excessive publishstate calls

(cherry picked from commit 61ad7bd0)
parent c5941442
...@@ -114,6 +114,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -114,6 +114,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{
private static final int SHOW_PROGRESS = 0; private static final int SHOW_PROGRESS = 0;
private static final int SHOW_TOAST = 1; private static final int SHOW_TOAST = 1;
private static final int END_MEDIASESSION = 2; private static final int END_MEDIASESSION = 2;
private static final int PUBLISH_STATE = 3;
private static final long DELAY_DOUBLE_CLICK = 800L; private static final long DELAY_DOUBLE_CLICK = 800L;
private static final long DELAY_LONG_CLICK = 1000L; private static final long DELAY_LONG_CLICK = 1000L;
...@@ -551,7 +552,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -551,7 +552,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{
break; break;
case MediaPlayer.Event.PositionChanged: case MediaPlayer.Event.PositionChanged:
updateWidgetPosition(event.getPositionChanged()); updateWidgetPosition(event.getPositionChanged());
publishState(); mHandler.sendEmptyMessage(PUBLISH_STATE);
break; break;
case MediaPlayer.Event.Vout: case MediaPlayer.Event.Vout:
break; break;
...@@ -616,6 +617,8 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -616,6 +617,8 @@ public class PlaybackService extends MediaBrowserServiceCompat{
private final Handler mHandler = new PlaybackServiceHandler(this); private final Handler mHandler = new PlaybackServiceHandler(this);
private static class PlaybackServiceHandler extends WeakHandler<PlaybackService> { private static class PlaybackServiceHandler extends WeakHandler<PlaybackService> {
private long lastPublicationDate = 0L;
PlaybackServiceHandler(PlaybackService fragment) { PlaybackServiceHandler(PlaybackService fragment) {
super(fragment); super(fragment);
} }
...@@ -643,6 +646,13 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -643,6 +646,13 @@ public class PlaybackService extends MediaBrowserServiceCompat{
case END_MEDIASESSION: case END_MEDIASESSION:
if (service.mMediaSession != null) service.mMediaSession.setActive(false); if (service.mMediaSession != null) service.mMediaSession.setActive(false);
break; break;
case PUBLISH_STATE:
final long time = System.currentTimeMillis();
if (time - lastPublicationDate > 1000L) {
service.publishState();
lastPublicationDate = time;
}
break;
} }
} }
} }
...@@ -1082,16 +1092,19 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -1082,16 +1092,19 @@ public class PlaybackService extends MediaBrowserServiceCompat{
pscb.addCustomAction("repeat", getString(R.string.repeat_title), repeatResId); pscb.addCustomAction("repeat", getString(R.string.repeat_title), repeatResId);
boolean mediaIsActive = state != PlaybackStateCompat.STATE_STOPPED; boolean mediaIsActive = state != PlaybackStateCompat.STATE_STOPPED;
boolean update = mMediaSession.isActive() != mediaIsActive;
mMediaSession.setPlaybackState(pscb.build()); mMediaSession.setPlaybackState(pscb.build());
mMediaSession.setActive(mediaIsActive); mMediaSession.setActive(mediaIsActive);
mMediaSession.setQueueTitle(getString(R.string.music_now_playing)); mMediaSession.setQueueTitle(getString(R.string.music_now_playing));
if (update) {
if (mediaIsActive) if (mediaIsActive)
sendStartSessionIdIntent(); sendStartSessionIdIntent();
else else
sendStopSessionIdIntent(); sendStopSessionIdIntent();
} }
}
private void notifyTrackChanged() { private void notifyTrackChanged() {
mHandler.sendEmptyMessage(SHOW_PROGRESS); mHandler.sendEmptyMessage(SHOW_PROGRESS);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment