Use corresponding profile to get active bluetooth device

- Do not use streamType to decide which active device should return.
  Base on b/80453878 comment#6, the steamType intent will only be sent
  if an action is made on stream volume or a media route is changed:
  For instance when a call to setStreamVolume() or getDeviceForStream() is made.
  It is not broadcast on actual routing changes.
  It should not be used as an indicator that the route changed during a call.
  There is no callback API and the only option is polling with getDeviceForStream().
- Use corresponding profile to get active bluetooth device
  instead of streamType
- Add test to verify the result of findActiveDevice()
  eg:
  1. A2dp device active, hearing aid device not active : return a2dp device
  2. A2dp device not active, hearing aid device not active : return null
  3. hfp device active, hearing aid device not active : return hfp device
  4. hfp device not active, hearing aid device not active : return null

Bug: 80453878
Test: make -j42 RunSettingsRoboTests
Change-Id: I5bd94899a5d508e60ce911da9689b727ad1fc20c
This commit is contained in:
hughchen
2018-07-05 14:59:57 +08:00
committed by Hugh Chen
parent 2061c2bda8
commit 244c7586f9
6 changed files with 94 additions and 130 deletions

View File

@@ -17,11 +17,6 @@
package com.android.settings.sound;
import static android.media.AudioManager.STREAM_DEVICES_CHANGED_ACTION;
import static android.media.AudioManager.STREAM_MUSIC;
import static android.media.AudioManager.STREAM_VOICE_CALL;
import static android.media.AudioSystem.DEVICE_OUT_ALL_A2DP;
import static android.media.AudioSystem.DEVICE_OUT_ALL_SCO;
import static android.media.AudioSystem.DEVICE_OUT_HEARING_AID;
import static android.media.MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY;
import android.bluetooth.BluetoothDevice;
@@ -309,28 +304,16 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
}
/**
* According to different stream and output device, find the active device from
* the corresponding profile. Hearing aid device could stream both STREAM_MUSIC
* and STREAM_VOICE_CALL.
*
* @param streamType the type of audio streams.
* @return the active device. Return null if the active device is current device
* or streamType is not STREAM_MUSIC or STREAM_VOICE_CALL.
* Find active hearing aid device
*/
protected BluetoothDevice findActiveDevice(int streamType) {
if (streamType != STREAM_MUSIC && streamType != STREAM_VOICE_CALL) {
return null;
}
if (isStreamFromOutputDevice(STREAM_MUSIC, DEVICE_OUT_ALL_A2DP)) {
return mProfileManager.getA2dpProfile().getActiveDevice();
} else if (isStreamFromOutputDevice(STREAM_VOICE_CALL, DEVICE_OUT_ALL_SCO)) {
return mProfileManager.getHeadsetProfile().getActiveDevice();
} else if (isStreamFromOutputDevice(streamType, DEVICE_OUT_HEARING_AID)) {
protected BluetoothDevice findActiveHearingAidDevice() {
final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
if (hearingAidProfile != null) {
// The first element is the left active device; the second element is
// the right active device. And they will have same hiSyncId. If either
// or both side is not active, it will be null on that position.
List<BluetoothDevice> activeDevices =
mProfileManager.getHearingAidProfile().getActiveDevices();
List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
for (BluetoothDevice btDevice : activeDevices) {
if (btDevice != null && mConnectedDevices.contains(btDevice)) {
// also need to check mConnectedDevices, because one of
@@ -342,6 +325,14 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
return null;
}
/**
* Find the active device from the corresponding profile.
*
* @return the active device. Return null if the
* corresponding profile don't have active device.
*/
public abstract BluetoothDevice findActiveDevice();
int getDefaultDeviceIndex() {
// Default device is after all connected devices.
return mConnectedDevices.size();