From c14e3d077c350c5414defea1607582887d7fce03 Mon Sep 17 00:00:00 2001 From: Adam Baker Date: Sun, 3 Jan 2016 20:03:52 +0000 Subject: [PATCH] Add Toasts that appear on volume change --- .../nutomic/controldlna/gui/RouteFragment.java | 17 +++++++++++++++++ .../controldlna/localroute/Controller.java | 4 ++-- .../controldlna/localroute/Provider.java | 3 ++- .../mediarouter/MediaRouterPlayService.java | 12 ++++++++++++ src/main/res/values/strings.xml | 1 + 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java b/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java index 698dfe0..b8904df 100644 --- a/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java +++ b/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java @@ -504,12 +504,29 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements enableTrackHighlight(); } + private Toast mVolumeToast; + + private void updateVolumeToast(String text) + { + // The local provider doesn't report correct values so don't use our toast + // (local provider can use the SHOW_UI option on setVolume itself which + // does show correct values) + if (mMediaRouterPlayService.isLocal()) + return; + if (mVolumeToast != null) + mVolumeToast.cancel(); + mVolumeToast=Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT); + mVolumeToast.show(); + } + public void increaseVolume() { mMediaRouterPlayService.increaseVolume(); + updateVolumeToast(mMediaRouterPlayService.getVolumeText()); } public void decreaseVolume() { mMediaRouterPlayService.decreaseVolume(); + updateVolumeToast(mMediaRouterPlayService.getVolumeText()); } /** diff --git a/src/main/java/com/github/nutomic/controldlna/localroute/Controller.java b/src/main/java/com/github/nutomic/controldlna/localroute/Controller.java index 474f59a..3f5caa4 100644 --- a/src/main/java/com/github/nutomic/controldlna/localroute/Controller.java +++ b/src/main/java/com/github/nutomic/controldlna/localroute/Controller.java @@ -92,13 +92,13 @@ public class Controller extends MediaRouteProvider.RouteController implements @Override public void onSetVolume(int volume) { - mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); + mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_SHOW_UI); } @Override public void onUpdateVolume(int delta) { int currentVolume = mAudio.getStreamVolume(AudioManager.STREAM_MUSIC); - mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume + delta, 0); + mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume + delta, AudioManager.FLAG_SHOW_UI); } @Override diff --git a/src/main/java/com/github/nutomic/controldlna/localroute/Provider.java b/src/main/java/com/github/nutomic/controldlna/localroute/Provider.java index 448848e..86db9c8 100644 --- a/src/main/java/com/github/nutomic/controldlna/localroute/Provider.java +++ b/src/main/java/com/github/nutomic/controldlna/localroute/Provider.java @@ -91,7 +91,8 @@ final class Provider extends MediaRouteProvider { .addControlFilters(CONTROL_FILTERS) .setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE) .setVolumeHandling(MediaRouter.RouteInfo.PLAYBACK_VOLUME_VARIABLE) - .setVolume(mAudio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) + .setVolume(mAudio.getStreamVolume(AudioManager.STREAM_MUSIC)) + .setVolumeMax(mAudio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) .build(); diff --git a/src/main/java/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java b/src/main/java/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java index 94b7548..4e0b7aa 100644 --- a/src/main/java/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java +++ b/src/main/java/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java @@ -496,6 +496,13 @@ public class MediaRouterPlayService extends Service { mMediaRouter.getSelectedRoute().requestUpdateVolume(-1); } + public String getVolumeText() + { + return String.format(getResources().getString(R.string.volume_text), + mMediaRouter.getSelectedRoute().getVolume(), + mMediaRouter.getSelectedRoute().getVolumeMax()); + } + public List getPlaylist() { return mPlaylist; } @@ -525,4 +532,9 @@ public class MediaRouterPlayService extends Service { return mCurrentRoute; } + public boolean isLocal() + { + return mCurrentRoute.getName().startsWith(getResources().getString(R.string.local_device)); + } + } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index b54aa5d..376db95 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -29,6 +29,7 @@ Please select a route Wifi needs to be connected for playback. Do you want to enable it now? + Volume: %1$d/%2$d Local Device