diff options
author | RoboErik <epastern@google.com> | 2014-07-14 13:40:43 -0700 |
---|---|---|
committer | RoboErik <epastern@google.com> | 2014-07-14 13:40:43 -0700 |
commit | c785a78fb483fe54012175c53d3758b2412de7b9 (patch) | |
tree | 381234db29ffa13f3f34972052d282978df86915 /tests/OneMedia/src/com/android/onemedia/PlayerSession.java | |
parent | 550116576cce028d3c435f7c3ae9f6e3b92b5cf2 (diff) | |
download | frameworks_base-c785a78fb483fe54012175c53d3758b2412de7b9.zip frameworks_base-c785a78fb483fe54012175c53d3758b2412de7b9.tar.gz frameworks_base-c785a78fb483fe54012175c53d3758b2412de7b9.tar.bz2 |
Make PlaybackState immutable with a builder
bug:15862252
Change-Id: I51f2e466bd2c41bbe80d20aa9785126a7ac6ab3f
Diffstat (limited to 'tests/OneMedia/src/com/android/onemedia/PlayerSession.java')
-rw-r--r-- | tests/OneMedia/src/com/android/onemedia/PlayerSession.java | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/tests/OneMedia/src/com/android/onemedia/PlayerSession.java b/tests/OneMedia/src/com/android/onemedia/PlayerSession.java index 7c0eabe..78353b2 100644 --- a/tests/OneMedia/src/com/android/onemedia/PlayerSession.java +++ b/tests/OneMedia/src/com/android/onemedia/PlayerSession.java @@ -28,6 +28,7 @@ import android.media.session.PlaybackState; import android.os.Bundle; import android.support.media.protocols.MediaPlayerProtocol; import android.support.media.protocols.MediaPlayerProtocol.MediaStatus; +import android.os.SystemClock; import android.util.Log; import android.view.KeyEvent; @@ -59,9 +60,9 @@ public class PlayerSession { mRenderer = new LocalRenderer(context, null); mCallback = new SessionCb(); mRenderListener = new RenderListener(); - mPlaybackState = new PlaybackState(); - mPlaybackState.setActions(PlaybackState.ACTION_PAUSE - | PlaybackState.ACTION_PLAY); + PlaybackState.Builder psBob = new PlaybackState.Builder(); + psBob.setActions(PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY); + mPlaybackState = psBob.build(); mRenderer.registerListener(mRenderListener); } @@ -131,7 +132,10 @@ public class PlayerSession { private void updateState(int newState) { float rate = newState == PlaybackState.STATE_PLAYING ? 1 : 0; long position = mRenderer == null ? -1 : mRenderer.getSeekPosition(); - mPlaybackState.setState(newState, position, rate); + PlaybackState.Builder bob = new PlaybackState.Builder(mPlaybackState); + bob.setState(newState, position, rate, SystemClock.elapsedRealtime()); + bob.setErrorMessage(null); + mPlaybackState = bob.build(); mSession.setPlaybackState(mPlaybackState); } @@ -144,10 +148,12 @@ public class PlayerSession { @Override public void onError(int type, int extra, Bundle extras, Throwable error) { Log.d(TAG, "Sending onError with type " + type + " and extra " + extra); - mPlaybackState.setState(PlaybackState.STATE_ERROR, -1, 0); + PlaybackState.Builder bob = new PlaybackState.Builder(mPlaybackState); + bob.setState(PlaybackState.STATE_ERROR, -1, 0, 0); if (error != null) { - mPlaybackState.setErrorMessage(error.getLocalizedMessage()); + bob.setErrorMessage(error.getLocalizedMessage()); } + mPlaybackState = bob.build(); mSession.setPlaybackState(mPlaybackState); if (mListener != null) { mListener.onPlayStateChanged(mPlaybackState); @@ -156,36 +162,41 @@ public class PlayerSession { @Override public void onStateChanged(int newState) { - if (newState != Renderer.STATE_ERROR) { - mPlaybackState.setErrorMessage(null); - } long position = -1; if (mRenderer != null) { position = mRenderer.getSeekPosition(); } + int pbState; + float rate = 0; + String errorMsg = null; switch (newState) { case Renderer.STATE_ENDED: case Renderer.STATE_STOPPED: - mPlaybackState.setState(PlaybackState.STATE_STOPPED, position, 0); + pbState = PlaybackState.STATE_STOPPED; break; case Renderer.STATE_INIT: case Renderer.STATE_PREPARING: - mPlaybackState.setState(PlaybackState.STATE_BUFFERING, position, 0); + pbState = PlaybackState.STATE_BUFFERING; break; case Renderer.STATE_ERROR: - mPlaybackState.setState(PlaybackState.STATE_ERROR, position, 0); + pbState = PlaybackState.STATE_ERROR; break; case Renderer.STATE_PAUSED: - mPlaybackState.setState(PlaybackState.STATE_PAUSED, position, 0); + pbState = PlaybackState.STATE_PAUSED; break; case Renderer.STATE_PLAYING: - mPlaybackState.setState(PlaybackState.STATE_PLAYING, position, 1); + pbState = PlaybackState.STATE_PLAYING; + rate = 1; break; default: - mPlaybackState.setState(PlaybackState.STATE_ERROR, position, 0); - mPlaybackState.setErrorMessage("unkown state"); + pbState = PlaybackState.STATE_ERROR; + errorMsg = "unknown state"; break; } + PlaybackState.Builder bob = new PlaybackState.Builder(mPlaybackState); + bob.setState(pbState, position, rate, SystemClock.elapsedRealtime()); + bob.setErrorMessage(errorMsg); + mPlaybackState = bob.build(); mSession.setPlaybackState(mPlaybackState); if (mListener != null) { mListener.onPlayStateChanged(mPlaybackState); @@ -200,7 +211,10 @@ public class PlayerSession { public void onFocusLost() { Log.d(TAG, "Focus lost, changing state to " + Renderer.STATE_PAUSED); long position = mRenderer == null ? -1 : mRenderer.getSeekPosition(); - mPlaybackState.setState(PlaybackState.STATE_PAUSED, position, 0); + PlaybackState.Builder bob = new PlaybackState.Builder(mPlaybackState); + bob.setState(PlaybackState.STATE_PAUSED, position, 0, SystemClock.elapsedRealtime()); + bob.setErrorMessage(null); + mPlaybackState = bob.build(); mSession.setPlaybackState(mPlaybackState); if (mListener != null) { mListener.onPlayStateChanged(mPlaybackState); |