From 048fad32f6ddb59fb345dc9484c5cc404b644aff Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 26 Sep 2013 00:19:15 +0200 Subject: [PATCH] Changed UpnpPlayer to store volume locally instead of always querying it. --- .../nutomic/controldlna/gui/MainActivity.java | 4 +- .../nutomic/controldlna/upnp/UpnpPlayer.java | 84 +++++++++---------- 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/com/github/nutomic/controldlna/gui/MainActivity.java b/src/com/github/nutomic/controldlna/gui/MainActivity.java index aef2cc3..fc934e0 100644 --- a/src/com/github/nutomic/controldlna/gui/MainActivity.java +++ b/src/com/github/nutomic/controldlna/gui/MainActivity.java @@ -143,11 +143,11 @@ public class MainActivity extends ActionBarActivity { switch (event.getKeyCode()) { case KeyEvent.KEYCODE_VOLUME_UP: if (event.getAction() == KeyEvent.ACTION_DOWN) - mPlayer.increaseVolume(); + mPlayer.changeVolume(1); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: if (event.getAction() == KeyEvent.ACTION_DOWN) - mPlayer.decreaseVolume(); + mPlayer.changeVolume(-1); return true; default: return super.dispatchKeyEvent(event); diff --git a/src/com/github/nutomic/controldlna/upnp/UpnpPlayer.java b/src/com/github/nutomic/controldlna/upnp/UpnpPlayer.java index 6ebb8f7..1f56d6d 100644 --- a/src/com/github/nutomic/controldlna/upnp/UpnpPlayer.java +++ b/src/com/github/nutomic/controldlna/upnp/UpnpPlayer.java @@ -57,11 +57,13 @@ public class UpnpPlayer extends UpnpController { private PlayServiceBinder mPlayService; - private long mMinVolume; + private int mMinVolume; - private long mMaxVolume; + private int mMaxVolume; - private long mVolumeStep; + private int mVolumeStep; + + private int mCurrentVolume; private ServiceConnection mPlayServiceConnection = new ServiceConnection() { @@ -100,13 +102,15 @@ public class UpnpPlayer extends UpnpController { /** * Sets an absolute volume. */ - public void setVolume(long newVolume) { + public void setVolume(int newVolume) { if (mPlayService.getService().getRenderer() == null) return; if (newVolume > mMaxVolume) newVolume = mMaxVolume; if (newVolume < mMinVolume) newVolume = mMinVolume; + mCurrentVolume = newVolume; + Log.d(TAG, "volume: " + Integer.toString(mCurrentVolume)); mUpnpService.getControlPoint().execute( new SetVolume(getService("RenderingControl"), newVolume) { @@ -125,9 +129,35 @@ public class UpnpPlayer extends UpnpController { * * @param amount Amount to change volume by (negative to lower volume). */ - private void changeVolume(final long amount) { - if (mPlayService.getService().getRenderer() == null) - return; + public void changeVolume(int delta) { + if (delta > 0 && delta < mVolumeStep) { + delta = mVolumeStep; + } + else if (delta < 0 && delta > -mVolumeStep) { + delta = -mVolumeStep; + } + setVolume(mCurrentVolume + delta); + } + + /** + * Selects the renderer for playback, applying its minimum and maximum volume. + */ + public void selectRenderer(Device renderer) { + mPlayService.getService().setRenderer(renderer); + + if (getService("RenderingControl").getStateVariable("Volume") != null) { + StateVariableAllowedValueRange volumeRange = + getService("RenderingControl").getStateVariable("Volume") + .getTypeDetails().getAllowedValueRange(); + mMinVolume = (int) volumeRange.getMinimum(); + mMaxVolume = (int) volumeRange.getMaximum(); + mVolumeStep = (int) volumeRange.getStep(); + } + else { + mMinVolume = 0; + mMaxVolume = 100; + mVolumeStep = 1; + } mUpnpService.getControlPoint().execute( new GetVolume(getService("RenderingControl")) { @@ -142,49 +172,11 @@ public class UpnpPlayer extends UpnpController { @SuppressWarnings("rawtypes") @Override public void received(ActionInvocation invocation, int currentVolume) { - setVolume(currentVolume + amount); + mCurrentVolume = currentVolume; } }); } - /** - * Increases the device volume by a minimum volume step. - */ - public void increaseVolume() { - changeVolume(mVolumeStep); - } - - /** - * Decreases the device volume by a minimum volume step. - */ - public void decreaseVolume() { - changeVolume(- mVolumeStep); - } - - /** - * Selects the renderer for playback, applying its minimum and maximum volume. - */ - public void selectRenderer(Device renderer) { - mPlayService.getService().setRenderer(renderer); - - if (getService("RenderingControl").getStateVariable("Volume") != null) { - StateVariableAllowedValueRange volumeRange = - getService("RenderingControl").getStateVariable("Volume") - .getTypeDetails().getAllowedValueRange(); - mMinVolume = volumeRange.getMinimum(); - mMaxVolume = volumeRange.getMaximum(); - mVolumeStep = volumeRange.getStep(); - } - else { - mMinVolume = 0; - mMaxVolume = 100; - mVolumeStep = 1; - } - // Hack, needed as using a smaller step seems to not - // increase volume on some devices. - mVolumeStep = 4; - } - /** * Seeks to the given absolute time in seconds. */