Sound + Output Switcher on Sound Setting

1. Show "play media to" item when Previously Connected device is available
2. Click "Play media to" to launch output slice
3. Update test case

Bug: 126475101
Test: make -j50 RunSettingsRoboTests
Change-Id: Id8afd1a2407acb78c11e81d2420ae8c16130a321
This commit is contained in:
timhypeng
2019-03-04 16:32:47 +08:00
committed by tim peng
parent 94240c5a52
commit abdf739071
8 changed files with 350 additions and 522 deletions

View File

@@ -16,13 +16,14 @@
package com.android.settings.sound;
import static android.bluetooth.IBluetoothHearingAid.HI_SYNC_ID_INVALID;
import static android.media.AudioManager.STREAM_MUSIC;
import static android.media.AudioSystem.DEVICE_OUT_REMOTE_SUBMIX;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.text.TextUtils;
import androidx.preference.Preference;
@@ -30,12 +31,16 @@ import com.android.settings.R;
import com.android.settingslib.Utils;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.media.MediaOutputSliceConstants;
import java.util.List;
/**
* This class which allows switching between A2dp-connected & HAP-connected BT devices.
* A few conditions will disable this switcher:
* - No available BT device(s)
* - Media stream captured by cast device
* This class allows launching MediaOutputSlice to switch output device.
* Preference would hide only when
* - Bluetooth = OFF
* - Bluetooth = ON and Connected Devices = 0 and Previously Connected = 0
* - Media stream captured by remote device
* - During a call.
*/
public class MediaOutputPreferenceController extends AudioSwitchPreferenceController {
@@ -66,40 +71,22 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
return;
}
mConnectedDevices.clear();
// Otherwise, list all of the A2DP connected device and display the active device.
if (mAudioManager.getMode() == AudioManager.MODE_NORMAL) {
mConnectedDevices.addAll(getConnectedA2dpDevices());
mConnectedDevices.addAll(getConnectedHearingAidDevices());
}
final int numDevices = mConnectedDevices.size();
mPreference.setVisible((numDevices == 0) ? false : true);
CharSequence[] mediaOutputs = new CharSequence[numDevices + 1];
CharSequence[] mediaValues = new CharSequence[numDevices + 1];
// Setup devices entries, select active connected device
setupPreferenceEntries(mediaOutputs, mediaValues, findActiveDevice());
// Display connected devices, default device and show the active device
setPreference(mediaOutputs, mediaValues, preference);
}
@Override
public void setActiveBluetoothDevice(BluetoothDevice device) {
if (mAudioManager.getMode() != AudioManager.MODE_NORMAL) {
return;
}
final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
if (hapProfile != null && a2dpProfile != null && device == null) {
hapProfile.setActiveDevice(null);
a2dpProfile.setActiveDevice(null);
} else if (hapProfile != null && hapProfile.getHiSyncId(device) != HI_SYNC_ID_INVALID) {
hapProfile.setActiveDevice(device);
} else if (a2dpProfile != null) {
a2dpProfile.setActiveDevice(device);
boolean deviceConnectable = false;
BluetoothDevice activeDevice = null;
// Show preference if there is connected or previously connected device
// Find active device and set its name as the preference's summary
List<BluetoothDevice> connectableA2dpDevices = getConnectableA2dpDevices();
List<BluetoothDevice> connectableHADevices = getConnectableHearingAidDevices();
if (mAudioManager.getMode() == AudioManager.MODE_NORMAL
&& ((connectableA2dpDevices != null && !connectableA2dpDevices.isEmpty())
|| (connectableHADevices != null && !connectableHADevices.isEmpty()))) {
deviceConnectable = true;
activeDevice = findActiveDevice();
}
mPreference.setVisible(deviceConnectable);
mPreference.setSummary((activeDevice == null) ?
mContext.getText(R.string.media_output_default_summary) :
activeDevice.getAliasName());
}
@Override
@@ -112,4 +99,34 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
}
return activeDevice;
}
/**
* Find active hearing aid device
*/
@Override
protected BluetoothDevice findActiveHearingAidDevice() {
final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
if (hearingAidProfile != null) {
List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
for (BluetoothDevice btDevice : activeDevices) {
if (btDevice != null) {
return btDevice;
}
}
}
return null;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
final Intent intent = new Intent()
.setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
return true;
}
return false;
}
}