AudioDeviceInventory: fix devices role cache purge.

Purging the devices role cache when a device is disconnected should be
handled differently whether the role request comes from an API or whether
it has been created internally. Requests coming from an API must not be
cancelled when the device is disconnected, whereas internal requests
must.
Created separate caches for internal and API requests.
Also fix a problem in AudioDeviceInventory.purgeRoles() where we cannot
rely on the connected device list (mConnectedDevices) because it does not
contain attached devices like a speaker.

Bug: 284468571
Test: repro steps in bug
Test: atest NonDefaultDeviceForStrategyTest
Test: atest AudioManagerTest#testPreferredDevicesForStrategy
Test: atest AudioManagerTest#testPreferredDeviceForCapturePreset

Change-Id: I04d620b7c494ce3275baa001fa6f6ed04857e889
This commit is contained in:
Eric Laurent
2023-05-31 15:02:05 +02:00
parent 6daf38c0e5
commit ace9601986
2 changed files with 106 additions and 48 deletions

View File

@@ -2205,19 +2205,19 @@ import java.util.concurrent.atomic.AtomicBoolean;
if (preferredCommunicationDevice == null) { if (preferredCommunicationDevice == null) {
AudioDeviceAttributes defaultDevice = getDefaultCommunicationDevice(); AudioDeviceAttributes defaultDevice = getDefaultCommunicationDevice();
if (defaultDevice != null) { if (defaultDevice != null) {
mDeviceInventory.setPreferredDevicesForStrategy( mDeviceInventory.setPreferredDevicesForStrategyInt(
mCommunicationStrategyId, Arrays.asList(defaultDevice)); mCommunicationStrategyId, Arrays.asList(defaultDevice));
mDeviceInventory.setPreferredDevicesForStrategy( mDeviceInventory.setPreferredDevicesForStrategyInt(
mAccessibilityStrategyId, Arrays.asList(defaultDevice)); mAccessibilityStrategyId, Arrays.asList(defaultDevice));
} else { } else {
mDeviceInventory.removePreferredDevicesForStrategy(mCommunicationStrategyId); mDeviceInventory.removePreferredDevicesForStrategInt(mCommunicationStrategyId);
mDeviceInventory.removePreferredDevicesForStrategy(mAccessibilityStrategyId); mDeviceInventory.removePreferredDevicesForStrategInt(mAccessibilityStrategyId);
} }
mDeviceInventory.applyConnectedDevicesRoles(); mDeviceInventory.applyConnectedDevicesRoles();
} else { } else {
mDeviceInventory.setPreferredDevicesForStrategy( mDeviceInventory.setPreferredDevicesForStrategyInt(
mCommunicationStrategyId, Arrays.asList(preferredCommunicationDevice)); mCommunicationStrategyId, Arrays.asList(preferredCommunicationDevice));
mDeviceInventory.setPreferredDevicesForStrategy( mDeviceInventory.setPreferredDevicesForStrategyInt(
mAccessibilityStrategyId, Arrays.asList(preferredCommunicationDevice)); mAccessibilityStrategyId, Arrays.asList(preferredCommunicationDevice));
} }
onUpdatePhoneStrategyDevice(preferredCommunicationDevice); onUpdatePhoneStrategyDevice(preferredCommunicationDevice);

View File

@@ -67,6 +67,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream;
/** /**
* Class to manage the inventory of all connected devices. * Class to manage the inventory of all connected devices.
@@ -324,14 +325,22 @@ public class AudioDeviceInventory {
mPreferredDevicesForCapturePreset.forEach((capturePreset, devices) -> { mPreferredDevicesForCapturePreset.forEach((capturePreset, devices) -> {
pw.println(" " + prefix + "capturePreset:" + capturePreset pw.println(" " + prefix + "capturePreset:" + capturePreset
+ " devices:" + devices); }); + " devices:" + devices); });
pw.println("\n" + prefix + "Applied devices roles for strategies:"); pw.println("\n" + prefix + "Applied devices roles for strategies (from API):");
mAppliedStrategyRoles.forEach((key, devices) -> { mAppliedStrategyRoles.forEach((key, devices) -> {
pw.println(" " + prefix + "strategy: " + key.first pw.println(" " + prefix + "strategy: " + key.first
+ " role:" + key.second + " devices:" + devices); }); + " role:" + key.second + " devices:" + devices); });
pw.println("\n" + prefix + "Applied devices roles for presets:"); pw.println("\n" + prefix + "Applied devices roles for strategies (internal):");
mAppliedStrategyRolesInt.forEach((key, devices) -> {
pw.println(" " + prefix + "strategy: " + key.first
+ " role:" + key.second + " devices:" + devices); });
pw.println("\n" + prefix + "Applied devices roles for presets (from API):");
mAppliedPresetRoles.forEach((key, devices) -> { mAppliedPresetRoles.forEach((key, devices) -> {
pw.println(" " + prefix + "preset: " + key.first pw.println(" " + prefix + "preset: " + key.first
+ " role:" + key.second + " devices:" + devices); }); + " role:" + key.second + " devices:" + devices); });
pw.println("\n" + prefix + "Applied devices roles for presets (internal:");
mAppliedPresetRolesInt.forEach((key, devices) -> {
pw.println(" " + prefix + "preset: " + key.first
+ " role:" + key.second + " devices:" + devices); });
} }
//------------------------------------------------------------ //------------------------------------------------------------
@@ -353,6 +362,9 @@ public class AudioDeviceInventory {
di.mDeviceCodecFormat); di.mDeviceCodecFormat);
} }
mAppliedStrategyRoles.clear(); mAppliedStrategyRoles.clear();
mAppliedStrategyRolesInt.clear();
mAppliedPresetRoles.clear();
mAppliedPresetRolesInt.clear();
applyConnectedDevicesRoles_l(); applyConnectedDevicesRoles_l();
} }
synchronized (mPreferredDevices) { synchronized (mPreferredDevices) {
@@ -361,8 +373,8 @@ public class AudioDeviceInventory {
} }
synchronized (mNonDefaultDevices) { synchronized (mNonDefaultDevices) {
mNonDefaultDevices.forEach((strategy, devices) -> { mNonDefaultDevices.forEach((strategy, devices) -> {
addDevicesRoleForStrategy( addDevicesRoleForStrategy(strategy, AudioSystem.DEVICE_ROLE_DISABLED,
strategy, AudioSystem.DEVICE_ROLE_DISABLED, devices); }); devices, false /* internal */); });
} }
synchronized (mPreferredDevicesForCapturePreset) { synchronized (mPreferredDevicesForCapturePreset) {
// TODO: call audiosystem to restore // TODO: call audiosystem to restore
@@ -768,7 +780,7 @@ public class AudioDeviceInventory {
} }
return status; return status;
} }
// Only used for external requests coming from an API
/*package*/ int setPreferredDevicesForStrategy(int strategy, /*package*/ int setPreferredDevicesForStrategy(int strategy,
@NonNull List<AudioDeviceAttributes> devices) { @NonNull List<AudioDeviceAttributes> devices) {
int status = AudioSystem.ERROR; int status = AudioSystem.ERROR;
@@ -777,10 +789,17 @@ public class AudioDeviceInventory {
"setPreferredDevicesForStrategy, strategy: " + strategy "setPreferredDevicesForStrategy, strategy: " + strategy
+ " devices: " + devices)).printLog(TAG)); + " devices: " + devices)).printLog(TAG));
status = setDevicesRoleForStrategy( status = setDevicesRoleForStrategy(
strategy, AudioSystem.DEVICE_ROLE_PREFERRED, devices); strategy, AudioSystem.DEVICE_ROLE_PREFERRED, devices, false /* internal */);
} }
return status; return status;
} }
// Only used for internal requests
/*package*/ int setPreferredDevicesForStrategyInt(int strategy,
@NonNull List<AudioDeviceAttributes> devices) {
return setDevicesRoleForStrategy(
strategy, AudioSystem.DEVICE_ROLE_PREFERRED, devices, true /* internal */);
}
/*package*/ int removePreferredDevicesForStrategyAndSave(int strategy) { /*package*/ int removePreferredDevicesForStrategyAndSave(int strategy) {
final int status = removePreferredDevicesForStrategy(strategy); final int status = removePreferredDevicesForStrategy(strategy);
@@ -789,7 +808,7 @@ public class AudioDeviceInventory {
} }
return status; return status;
} }
// Only used for external requests coming from an API
/*package*/ int removePreferredDevicesForStrategy(int strategy) { /*package*/ int removePreferredDevicesForStrategy(int strategy) {
int status = AudioSystem.ERROR; int status = AudioSystem.ERROR;
@@ -799,10 +818,15 @@ public class AudioDeviceInventory {
+ strategy)).printLog(TAG)); + strategy)).printLog(TAG));
status = clearDevicesRoleForStrategy( status = clearDevicesRoleForStrategy(
strategy, AudioSystem.DEVICE_ROLE_PREFERRED); strategy, AudioSystem.DEVICE_ROLE_PREFERRED, false /*internal */);
} }
return status; return status;
} }
// Only used for internal requests
/*package*/ int removePreferredDevicesForStrategInt(int strategy) {
return clearDevicesRoleForStrategy(
strategy, AudioSystem.DEVICE_ROLE_PREFERRED, true /*internal */);
}
/*package*/ int setDeviceAsNonDefaultForStrategyAndSave(int strategy, /*package*/ int setDeviceAsNonDefaultForStrategyAndSave(int strategy,
@NonNull AudioDeviceAttributes device) { @NonNull AudioDeviceAttributes device) {
@@ -816,7 +840,7 @@ public class AudioDeviceInventory {
"setDeviceAsNonDefaultForStrategyAndSave, strategy: " + strategy "setDeviceAsNonDefaultForStrategyAndSave, strategy: " + strategy
+ " device: " + device)).printLog(TAG)); + " device: " + device)).printLog(TAG));
status = addDevicesRoleForStrategy( status = addDevicesRoleForStrategy(
strategy, AudioSystem.DEVICE_ROLE_DISABLED, devices); strategy, AudioSystem.DEVICE_ROLE_DISABLED, devices, false /* internal */);
} }
if (status == AudioSystem.SUCCESS) { if (status == AudioSystem.SUCCESS) {
@@ -838,7 +862,7 @@ public class AudioDeviceInventory {
+ strategy + " devices: " + device)).printLog(TAG)); + strategy + " devices: " + device)).printLog(TAG));
status = removeDevicesRoleForStrategy( status = removeDevicesRoleForStrategy(
strategy, AudioSystem.DEVICE_ROLE_DISABLED, devices); strategy, AudioSystem.DEVICE_ROLE_DISABLED, devices, false /* internal */);
} }
if (status == AudioSystem.SUCCESS) { if (status == AudioSystem.SUCCESS) {
@@ -877,6 +901,7 @@ public class AudioDeviceInventory {
return status; return status;
} }
// Only used for external requests coming from an API
private int setPreferredDevicesForCapturePreset( private int setPreferredDevicesForCapturePreset(
int capturePreset, @NonNull List<AudioDeviceAttributes> devices) { int capturePreset, @NonNull List<AudioDeviceAttributes> devices) {
int status = AudioSystem.ERROR; int status = AudioSystem.ERROR;
@@ -895,6 +920,7 @@ public class AudioDeviceInventory {
return status; return status;
} }
// Only used for external requests coming from an API
private int clearPreferredDevicesForCapturePreset(int capturePreset) { private int clearPreferredDevicesForCapturePreset(int capturePreset) {
int status = AudioSystem.ERROR; int status = AudioSystem.ERROR;
@@ -905,20 +931,23 @@ public class AudioDeviceInventory {
return status; return status;
} }
private int addDevicesRoleForCapturePreset(int capturePreset, int role, // Only used for internal requests
private int addDevicesRoleForCapturePresetInt(int capturePreset, int role,
@NonNull List<AudioDeviceAttributes> devices) { @NonNull List<AudioDeviceAttributes> devices) {
return addDevicesRole(mAppliedPresetRoles, (p, r, d) -> { return addDevicesRole(mAppliedPresetRolesInt, (p, r, d) -> {
return mAudioSystem.addDevicesRoleForCapturePreset(p, r, d); return mAudioSystem.addDevicesRoleForCapturePreset(p, r, d);
}, capturePreset, role, devices); }, capturePreset, role, devices);
} }
private int removeDevicesRoleForCapturePreset(int capturePreset, int role, // Only used for internal requests
private int removeDevicesRoleForCapturePresetInt(int capturePreset, int role,
@NonNull List<AudioDeviceAttributes> devices) { @NonNull List<AudioDeviceAttributes> devices) {
return removeDevicesRole(mAppliedPresetRoles, (p, r, d) -> { return removeDevicesRole(mAppliedPresetRolesInt, (p, r, d) -> {
return mAudioSystem.removeDevicesRoleForCapturePreset(p, r, d); return mAudioSystem.removeDevicesRoleForCapturePreset(p, r, d);
}, capturePreset, role, devices); }, capturePreset, role, devices);
} }
// Only used for external requests coming from an API
private int setDevicesRoleForCapturePreset(int capturePreset, int role, private int setDevicesRoleForCapturePreset(int capturePreset, int role,
@NonNull List<AudioDeviceAttributes> devices) { @NonNull List<AudioDeviceAttributes> devices) {
return setDevicesRole(mAppliedPresetRoles, (p, r, d) -> { return setDevicesRole(mAppliedPresetRoles, (p, r, d) -> {
@@ -928,6 +957,7 @@ public class AudioDeviceInventory {
}, capturePreset, role, devices); }, capturePreset, role, devices);
} }
// Only used for external requests coming from an API
private int clearDevicesRoleForCapturePreset(int capturePreset, int role) { private int clearDevicesRoleForCapturePreset(int capturePreset, int role) {
return clearDevicesRole(mAppliedPresetRoles, (p, r, d) -> { return clearDevicesRole(mAppliedPresetRoles, (p, r, d) -> {
return mAudioSystem.clearDevicesRoleForCapturePreset(p, r); return mAudioSystem.clearDevicesRoleForCapturePreset(p, r);
@@ -945,30 +975,37 @@ public class AudioDeviceInventory {
} }
private int addDevicesRoleForStrategy(int strategy, int role, private int addDevicesRoleForStrategy(int strategy, int role,
@NonNull List<AudioDeviceAttributes> devices) { @NonNull List<AudioDeviceAttributes> devices,
return addDevicesRole(mAppliedStrategyRoles, (s, r, d) -> { boolean internal) {
return addDevicesRole(internal ? mAppliedStrategyRolesInt : mAppliedStrategyRoles,
(s, r, d) -> {
return mAudioSystem.setDevicesRoleForStrategy(s, r, d); return mAudioSystem.setDevicesRoleForStrategy(s, r, d);
}, strategy, role, devices); }, strategy, role, devices);
} }
private int removeDevicesRoleForStrategy(int strategy, int role, private int removeDevicesRoleForStrategy(int strategy, int role,
@NonNull List<AudioDeviceAttributes> devices) { @NonNull List<AudioDeviceAttributes> devices,
return removeDevicesRole(mAppliedStrategyRoles, (s, r, d) -> { boolean internal) {
return removeDevicesRole(internal ? mAppliedStrategyRolesInt : mAppliedStrategyRoles,
(s, r, d) -> {
return mAudioSystem.removeDevicesRoleForStrategy(s, r, d); return mAudioSystem.removeDevicesRoleForStrategy(s, r, d);
}, strategy, role, devices); }, strategy, role, devices);
} }
private int setDevicesRoleForStrategy(int strategy, int role, private int setDevicesRoleForStrategy(int strategy, int role,
@NonNull List<AudioDeviceAttributes> devices) { @NonNull List<AudioDeviceAttributes> devices,
return setDevicesRole(mAppliedStrategyRoles, (s, r, d) -> { boolean internal) {
return setDevicesRole(internal ? mAppliedStrategyRolesInt : mAppliedStrategyRoles,
(s, r, d) -> {
return mAudioSystem.setDevicesRoleForStrategy(s, r, d); return mAudioSystem.setDevicesRoleForStrategy(s, r, d);
}, (s, r, d) -> { }, (s, r, d) -> {
return mAudioSystem.clearDevicesRoleForStrategy(s, r); return mAudioSystem.clearDevicesRoleForStrategy(s, r);
}, strategy, role, devices); }, strategy, role, devices);
} }
private int clearDevicesRoleForStrategy(int strategy, int role) { private int clearDevicesRoleForStrategy(int strategy, int role, boolean internal) {
return clearDevicesRole(mAppliedStrategyRoles, (s, r, d) -> { return clearDevicesRole(internal ? mAppliedStrategyRolesInt : mAppliedStrategyRoles,
(s, r, d) -> {
return mAudioSystem.clearDevicesRoleForStrategy(s, r); return mAudioSystem.clearDevicesRoleForStrategy(s, r);
}, strategy, role); }, strategy, role);
} }
@@ -978,16 +1015,25 @@ public class AudioDeviceInventory {
// same list of devices for a given role and strategy and the corresponding systematic // same list of devices for a given role and strategy and the corresponding systematic
// redundant work in audio policy manager and audio flinger. // redundant work in audio policy manager and audio flinger.
// The key is the pair <Strategy , Role> and the value is the current list of devices. // The key is the pair <Strategy , Role> and the value is the current list of devices.
// mAppliedStrategyRoles is for requests coming from an API.
// mAppliedStrategyRolesInt is for internal requests. Entries are removed when the requested
// device is disconnected.
private final ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>> private final ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>>
mAppliedStrategyRoles = new ArrayMap<>(); mAppliedStrategyRoles = new ArrayMap<>();
private final ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>>
mAppliedStrategyRolesInt = new ArrayMap<>();
// Cache for applied roles for capture presets and devices. The cache avoids reapplying the // Cache for applied roles for capture presets and devices. The cache avoids reapplying the
// same list of devices for a given role and capture preset and the corresponding systematic // same list of devices for a given role and capture preset and the corresponding systematic
// redundant work in audio policy manager and audio flinger. // redundant work in audio policy manager and audio flinger.
// The key is the pair <Preset , Role> and the value is the current list of devices. // The key is the pair <Preset , Role> and the value is the current list of devices.
// mAppliedPresetRoles is for requests coming from an API.
// mAppliedPresetRolesInt is for internal requests. Entries are removed when the requested
// device is disconnected.
private final ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>> private final ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>>
mAppliedPresetRoles = new ArrayMap<>(); mAppliedPresetRoles = new ArrayMap<>();
private final ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>>
mAppliedPresetRolesInt = new ArrayMap<>();
interface AudioSystemInterface { interface AudioSystemInterface {
int deviceRoleAction(int usecase, int role, @Nullable List<AudioDeviceAttributes> devices); int deviceRoleAction(int usecase, int role, @Nullable List<AudioDeviceAttributes> devices);
@@ -1113,9 +1159,9 @@ public class AudioDeviceInventory {
@GuardedBy("mDevicesLock") @GuardedBy("mDevicesLock")
private void purgeDevicesRoles_l() { private void purgeDevicesRoles_l() {
purgeRoles(mAppliedStrategyRoles, (s, r, d) -> { purgeRoles(mAppliedStrategyRolesInt, (s, r, d) -> {
return mAudioSystem.removeDevicesRoleForStrategy(s, r, d); }); return mAudioSystem.removeDevicesRoleForStrategy(s, r, d); });
purgeRoles(mAppliedPresetRoles, (p, r, d) -> { purgeRoles(mAppliedPresetRolesInt, (p, r, d) -> {
return mAudioSystem.removeDevicesRoleForCapturePreset(p, r, d); }); return mAudioSystem.removeDevicesRoleForCapturePreset(p, r, d); });
} }
@@ -1124,8 +1170,12 @@ public class AudioDeviceInventory {
ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>> rolesMap, ArrayMap<Pair<Integer, Integer>, List<AudioDeviceAttributes>> rolesMap,
AudioSystemInterface asi) { AudioSystemInterface asi) {
synchronized (rolesMap) { synchronized (rolesMap) {
AudioDeviceInfo[] connectedDevices = AudioManager.getDevicesStatic(
AudioManager.GET_DEVICES_ALL);
Iterator<Map.Entry<Pair<Integer, Integer>, List<AudioDeviceAttributes>>> itRole = Iterator<Map.Entry<Pair<Integer, Integer>, List<AudioDeviceAttributes>>> itRole =
rolesMap.entrySet().iterator(); rolesMap.entrySet().iterator();
while (itRole.hasNext()) { while (itRole.hasNext()) {
Map.Entry<Pair<Integer, Integer>, List<AudioDeviceAttributes>> entry = Map.Entry<Pair<Integer, Integer>, List<AudioDeviceAttributes>> entry =
itRole.next(); itRole.next();
@@ -1133,9 +1183,15 @@ public class AudioDeviceInventory {
Iterator<AudioDeviceAttributes> itDev = rolesMap.get(keyRole).iterator(); Iterator<AudioDeviceAttributes> itDev = rolesMap.get(keyRole).iterator();
while (itDev.hasNext()) { while (itDev.hasNext()) {
AudioDeviceAttributes ada = itDev.next(); AudioDeviceAttributes ada = itDev.next();
final String devKey = DeviceInfo.makeDeviceListKey(ada.getInternalType(),
ada.getAddress()); AudioDeviceInfo device = Stream.of(connectedDevices)
if (mConnectedDevices.get(devKey) == null) { .filter(d -> d.getInternalType() == ada.getInternalType())
.filter(d -> (!AudioSystem.isBluetoothDevice(d.getInternalType())
|| (d.getAddress() == ada.getAddress())))
.findFirst()
.orElse(null);
if (device == null) {
asi.deviceRoleAction(keyRole.first, keyRole.second, Arrays.asList(ada)); asi.deviceRoleAction(keyRole.first, keyRole.second, Arrays.asList(ada));
itDev.remove(); itDev.remove();
} }
@@ -1582,10 +1638,12 @@ public class AudioDeviceInventory {
} }
if (disable) { if (disable) {
addDevicesRoleForStrategy(strategy.getId(), addDevicesRoleForStrategy(strategy.getId(),
AudioSystem.DEVICE_ROLE_DISABLED, Arrays.asList(ada)); AudioSystem.DEVICE_ROLE_DISABLED,
Arrays.asList(ada), true /* internal */);
} else { } else {
removeDevicesRoleForStrategy(strategy.getId(), removeDevicesRoleForStrategy(strategy.getId(),
AudioSystem.DEVICE_ROLE_DISABLED, Arrays.asList(ada)); AudioSystem.DEVICE_ROLE_DISABLED,
Arrays.asList(ada), true /* internal */);
} }
} }
} }
@@ -1602,10 +1660,10 @@ public class AudioDeviceInventory {
+ ", disable: " + disable); + ", disable: " + disable);
} }
if (disable) { if (disable) {
addDevicesRoleForCapturePreset(capturePreset, addDevicesRoleForCapturePresetInt(capturePreset,
AudioSystem.DEVICE_ROLE_DISABLED, Arrays.asList(ada)); AudioSystem.DEVICE_ROLE_DISABLED, Arrays.asList(ada));
} else { } else {
removeDevicesRoleForCapturePreset(capturePreset, removeDevicesRoleForCapturePresetInt(capturePreset,
AudioSystem.DEVICE_ROLE_DISABLED, Arrays.asList(ada)); AudioSystem.DEVICE_ROLE_DISABLED, Arrays.asList(ada));
} }
} }