Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37c132e3 authored by Jeremy Wu's avatar Jeremy Wu Committed by Android (Google) Code Review
Browse files

Merge changes Iad90a1bf,I0f6e4c2e into main

* changes:
  ADI: remove opposite role
  ADI: make pref/nondef devs mutable before update
parents bafafd87 9202dd9f
Loading
Loading
Loading
Loading
+39 −10
Original line number Diff line number Diff line
@@ -90,8 +90,10 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
@@ -1381,8 +1383,13 @@ public class AudioDeviceInventory {
    private void saveSetPreferredDevices(int strategy,
                                               @NonNull List<AudioDeviceAttributes> devices) {
        mPreferredDevices.put(strategy, devices);
        List<AudioDeviceAttributes> nonDefaultDevices = mNonDefaultDevices.get(strategy);
        if (nonDefaultDevices != null) {

        List<AudioDeviceAttributes> nonDefaultDevices =
                Optional.ofNullable(mNonDefaultDevices.get(strategy))
                        .map(ArrayList::new)
                        .orElseGet(ArrayList::new);

        if (!nonDefaultDevices.isEmpty()) {
            nonDefaultDevices.removeAll(devices);

            if (nonDefaultDevices.isEmpty()) {
@@ -1405,10 +1412,10 @@ public class AudioDeviceInventory {
    @GuardedBy("mDevicesLock")
    private void saveSetDeviceAsNonDefault(int strategy,
                                                 @NonNull AudioDeviceAttributes device) {
        List<AudioDeviceAttributes> nonDefaultDevices = mNonDefaultDevices.get(strategy);
        if (nonDefaultDevices == null) {
            nonDefaultDevices = new ArrayList<>();
        }
        List<AudioDeviceAttributes> nonDefaultDevices =
                Optional.ofNullable(mNonDefaultDevices.get(strategy))
                        .map(ArrayList::new)
                        .orElseGet(ArrayList::new);

        if (!nonDefaultDevices.contains(device)) {
            nonDefaultDevices.add(device);
@@ -1417,9 +1424,12 @@ public class AudioDeviceInventory {
        mNonDefaultDevices.put(strategy, nonDefaultDevices);
        dispatchNonDefaultDevice(strategy, nonDefaultDevices);

        List<AudioDeviceAttributes> preferredDevices = mPreferredDevices.get(strategy);
        List<AudioDeviceAttributes> preferredDevices =
                Optional.ofNullable(mPreferredDevices.get(strategy))
                        .map(ArrayList::new)
                        .orElseGet(ArrayList::new);

        if (preferredDevices != null) {
        if (!preferredDevices.isEmpty()) {
            preferredDevices.remove(device);
            mPreferredDevices.put(strategy, preferredDevices);

@@ -1430,8 +1440,12 @@ public class AudioDeviceInventory {
    @GuardedBy("mDevicesLock")
    private void saveRemoveDeviceAsNonDefault(int strategy,
                                                    @NonNull AudioDeviceAttributes device) {
        List<AudioDeviceAttributes> nonDefaultDevices = mNonDefaultDevices.get(strategy);
        if (nonDefaultDevices != null) {
        List<AudioDeviceAttributes> nonDefaultDevices =
                Optional.ofNullable(mNonDefaultDevices.get(strategy))
                        .map(ArrayList::new)
                        .orElseGet(ArrayList::new);

        if (!nonDefaultDevices.isEmpty()) {
            nonDefaultDevices.remove(device);
            mNonDefaultDevices.put(strategy, nonDefaultDevices);
            dispatchNonDefaultDevice(strategy, nonDefaultDevices);
@@ -1846,6 +1860,21 @@ public class AudioDeviceInventory {
                status = addOp.deviceRoleAction(useCase, role, devices);
                if (status == AudioSystem.SUCCESS) {
                    rolesMap.put(key, new ArrayList(devices));
                    // Mirrors the behavior in EngineBase::setDevicesRoleForT() to ensure a
                    // preferred device is not also disabled, and vice-versa.
                    if (role != AudioSystem.DEVICE_ROLE_NONE) {
                        int roleToRemove = role == AudioSystem.DEVICE_ROLE_PREFERRED
                                ? AudioSystem.DEVICE_ROLE_DISABLED
                                : AudioSystem.DEVICE_ROLE_PREFERRED;
                        Pair<Integer, Integer> oppositeKey = new Pair<>(useCase, roleToRemove);
                        List<AudioDeviceAttributes> oppositeDevs = rolesMap.get(oppositeKey);
                        if (oppositeDevs != null) {
                            List<AudioDeviceAttributes> filteredDevs = oppositeDevs.stream()
                                    .filter(e -> !devices.contains(e))
                                    .collect(Collectors.toList());
                            rolesMap.put(oppositeKey, filteredDevs);
                        }
                    }
                }
            }
            return status;