Add Toasts that appear on volume change

This commit is contained in:
Adam Baker 2016-01-03 20:03:52 +00:00
parent c7f481d5ba
commit c14e3d077c
5 changed files with 34 additions and 3 deletions

View file

@ -504,12 +504,29 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
enableTrackHighlight(); 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() { public void increaseVolume() {
mMediaRouterPlayService.increaseVolume(); mMediaRouterPlayService.increaseVolume();
updateVolumeToast(mMediaRouterPlayService.getVolumeText());
} }
public void decreaseVolume() { public void decreaseVolume() {
mMediaRouterPlayService.decreaseVolume(); mMediaRouterPlayService.decreaseVolume();
updateVolumeToast(mMediaRouterPlayService.getVolumeText());
} }
/** /**

View file

@ -92,13 +92,13 @@ public class Controller extends MediaRouteProvider.RouteController implements
@Override @Override
public void onSetVolume(int volume) { public void onSetVolume(int volume) {
mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_SHOW_UI);
} }
@Override @Override
public void onUpdateVolume(int delta) { public void onUpdateVolume(int delta) {
int currentVolume = mAudio.getStreamVolume(AudioManager.STREAM_MUSIC); 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 @Override

View file

@ -91,7 +91,8 @@ final class Provider extends MediaRouteProvider {
.addControlFilters(CONTROL_FILTERS) .addControlFilters(CONTROL_FILTERS)
.setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE) .setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE)
.setVolumeHandling(MediaRouter.RouteInfo.PLAYBACK_VOLUME_VARIABLE) .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(); .build();

View file

@ -496,6 +496,13 @@ public class MediaRouterPlayService extends Service {
mMediaRouter.getSelectedRoute().requestUpdateVolume(-1); mMediaRouter.getSelectedRoute().requestUpdateVolume(-1);
} }
public String getVolumeText()
{
return String.format(getResources().getString(R.string.volume_text),
mMediaRouter.getSelectedRoute().getVolume(),
mMediaRouter.getSelectedRoute().getVolumeMax());
}
public List<Item> getPlaylist() { public List<Item> getPlaylist() {
return mPlaylist; return mPlaylist;
} }
@ -525,4 +532,9 @@ public class MediaRouterPlayService extends Service {
return mCurrentRoute; return mCurrentRoute;
} }
public boolean isLocal()
{
return mCurrentRoute.getName().startsWith(getResources().getString(R.string.local_device));
}
} }

View file

@ -29,6 +29,7 @@
<string name="select_route">Please select a route</string> <string name="select_route">Please select a route</string>
<string name="enable_wifi_dialog">Wifi needs to be connected for playback. <string name="enable_wifi_dialog">Wifi needs to be connected for playback.
Do you want to enable it now?</string> Do you want to enable it now?</string>
<string name="volume_text">Volume: %1$d/%2$d</string>
<!-- Description for local playback device --> <!-- Description for local playback device -->
<string name="local_device">Local Device</string> <string name="local_device">Local Device</string>