Merge "Set the current/max volume of system routes"

This commit is contained in:
Hyundo Moon
2020-02-07 08:45:25 +00:00
committed by Android (Google) Code Review
3 changed files with 72 additions and 8 deletions

View File

@@ -471,7 +471,8 @@ public class MediaRouter2 {
synchronized (sRouterLock) {
for (MediaRoute2Info route : routes) {
mRoutes.put(route.getId(), route);
if (route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
if (route.isSystemRoute()
|| route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
addedRoutes.add(route);
}
}
@@ -487,7 +488,8 @@ public class MediaRouter2 {
synchronized (sRouterLock) {
for (MediaRoute2Info route : routes) {
mRoutes.remove(route.getId());
if (route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
if (route.isSystemRoute()
|| route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
removedRoutes.add(route);
}
}
@@ -503,7 +505,8 @@ public class MediaRouter2 {
synchronized (sRouterLock) {
for (MediaRoute2Info route : routes) {
mRoutes.put(route.getId(), route);
if (route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
if (route.isSystemRoute()
|| route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
changedRoutes.add(route);
}
}
@@ -643,8 +646,8 @@ public class MediaRouter2 {
private List<MediaRoute2Info> filterRoutes(List<MediaRoute2Info> routes,
RouteDiscoveryPreference discoveryRequest) {
return routes.stream()
.filter(
route -> route.hasAnyFeatures(discoveryRequest.getPreferredFeatures()))
.filter(route -> route.isSystemRoute()
|| route.hasAnyFeatures(discoveryRequest.getPreferredFeatures()))
.collect(Collectors.toList());
}

View File

@@ -27,6 +27,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaRoute2Info;
import android.text.TextUtils;
import android.util.Slog;
@@ -55,6 +56,7 @@ class BluetoothRouteProvider {
private final Context mContext;
private final BluetoothAdapter mBluetoothAdapter;
private final BluetoothRoutesUpdatedListener mListener;
private final AudioManager mAudioManager;
private final Map<String, BluetoothEventReceiver> mEventReceiverMap = new HashMap<>();
private final IntentFilter mIntentFilter = new IntentFilter();
private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver();
@@ -82,6 +84,7 @@ class BluetoothRouteProvider {
mContext = context;
mBluetoothAdapter = btAdapter;
mListener = listener;
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
buildBluetoothRoutes();
mBluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.A2DP);
@@ -181,12 +184,15 @@ class BluetoothRouteProvider {
private BluetoothRouteInfo createBluetoothRoute(BluetoothDevice device) {
BluetoothRouteInfo newBtRoute = new BluetoothRouteInfo();
newBtRoute.btDevice = device;
// Current / Max volume will be set when connected.
// TODO: Is there any BT device which has fixed volume?
newBtRoute.route = new MediaRoute2Info.Builder(device.getAddress(), device.getName())
.addFeature(MediaRoute2Info.FEATURE_LIVE_AUDIO)
.setConnectionState(MediaRoute2Info.CONNECTION_STATE_DISCONNECTED)
.setDescription(mContext.getResources().getText(
R.string.bluetooth_a2dp_audio_route_name).toString())
.setDeviceType(MediaRoute2Info.DEVICE_TYPE_BLUETOOTH)
.setVolumeHandling(MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE)
.build();
newBtRoute.connectedProfiles = new SparseBooleanArray();
return newBtRoute;
@@ -203,10 +209,20 @@ class BluetoothRouteProvider {
Slog.w(TAG, "setRouteConnectionStateForDevice: route shouldn't be null");
return;
}
if (btRoute.route.getConnectionState() != state) {
btRoute.route = new MediaRoute2Info.Builder(btRoute.route)
.setConnectionState(state).build();
if (btRoute.route.getConnectionState() == state) {
return;
}
// Update volume when the connection state is changed.
MediaRoute2Info.Builder builder = new MediaRoute2Info.Builder(btRoute.route)
.setConnectionState(state);
if (state == MediaRoute2Info.CONNECTION_STATE_CONNECTED) {
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
builder.setVolumeMax(maxVolume).setVolume(currentVolume);
}
btRoute.route = builder.build();
}
interface BluetoothRoutesUpdatedListener {

View File

@@ -20,9 +20,11 @@ import static android.media.MediaRoute2Info.FEATURE_LIVE_AUDIO;
import static android.media.MediaRoute2Info.FEATURE_LIVE_VIDEO;
import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.AudioRoutesInfo;
import android.media.IAudioRoutesObserver;
@@ -107,6 +109,9 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
}
});
initializeSessionInfo();
mContext.registerReceiver(new VolumeChangeReceiver(),
new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION));
}
@Override
@@ -278,4 +283,44 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
}
mCallback.onSessionUpdated(this, sessionInfo);
}
private class VolumeChangeReceiver extends BroadcastReceiver {
// This will be called in the main thread.
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(AudioManager.VOLUME_CHANGED_ACTION)) {
return;
}
final int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
if (streamType != AudioManager.STREAM_MUSIC) {
return;
}
final int newVolume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0);
final int oldVolume = intent.getIntExtra(
AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, 0);
if (newVolume != oldVolume) {
String activeBtDeviceAddress = mBtRouteProvider.getActiveDeviceAddress();
if (!TextUtils.isEmpty(activeBtDeviceAddress)) {
for (int i = mBluetoothRoutes.size() - 1; i >= 0; i--) {
MediaRoute2Info route = mBluetoothRoutes.get(i);
if (TextUtils.equals(activeBtDeviceAddress, route.getId())) {
mBluetoothRoutes.set(i,
new MediaRoute2Info.Builder(route)
.setVolume(newVolume)
.build());
break;
}
}
} else {
mDefaultRoute = new MediaRoute2Info.Builder(mDefaultRoute)
.setVolume(newVolume)
.build();
}
publishRoutes();
}
}
}
}