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

Commit f4b71463 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "le_audio: Replace temporary context policy with direction for group" into tm-qpr-dev

parents 215db2f9 275d9034
Loading
Loading
Loading
Loading
+31 −80
Original line number Diff line number Diff line
@@ -92,6 +92,11 @@ public class LeAudioService extends ProfileService {
    private static final int MAX_LE_AUDIO_STATE_MACHINES = 10;
    private static LeAudioService sLeAudioService;

    /**
     * Indicates group audio support for none direction
     */
    private static final int AUDIO_DIRECTION_NONE = 0x00;

    /**
     * Indicates group audio support for input direction
     */
@@ -102,11 +107,6 @@ public class LeAudioService extends ProfileService {
     */
    private static final int AUDIO_DIRECTION_OUTPUT_BIT = 0x02;

    /*
     * Indicates no active contexts
     */
    private static final int ACTIVE_CONTEXTS_NONE = 0;

    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
@@ -137,14 +137,14 @@ public class LeAudioService extends ProfileService {
        LeAudioGroupDescriptor() {
            mIsConnected = false;
            mIsActive = false;
            mActiveContexts = ACTIVE_CONTEXTS_NONE;
            mDirection = AUDIO_DIRECTION_NONE;
            mCodecStatus = null;
            mLostLeadDeviceWhileStreaming = null;
        }

        public Boolean mIsConnected;
        public Boolean mIsActive;
        public Integer mActiveContexts;
        public Integer mDirection;
        public BluetoothLeAudioCodecStatus mCodecStatus;
        /* This can be non empty only for the streaming time */
        BluetoothDevice mLostLeadDeviceWhileStreaming;
@@ -161,21 +161,6 @@ public class LeAudioService extends ProfileService {
    private final Map<BluetoothDevice, Integer> mDeviceGroupIdMap = new ConcurrentHashMap<>();
    private final Map<BluetoothDevice, Integer> mDeviceAudioLocationMap = new ConcurrentHashMap<>();

    private final int mContextSupportingInputAudio = BluetoothLeAudio.CONTEXT_TYPE_CONVERSATIONAL
            | BluetoothLeAudio.CONTEXT_TYPE_VOICE_ASSISTANTS;

    private final int mContextSupportingOutputAudio = BluetoothLeAudio.CONTEXT_TYPE_CONVERSATIONAL
            | BluetoothLeAudio.CONTEXT_TYPE_MEDIA
            | BluetoothLeAudio.CONTEXT_TYPE_GAME
            | BluetoothLeAudio.CONTEXT_TYPE_INSTRUCTIONAL
            | BluetoothLeAudio.CONTEXT_TYPE_VOICE_ASSISTANTS
            | BluetoothLeAudio.CONTEXT_TYPE_LIVE
            | BluetoothLeAudio.CONTEXT_TYPE_SOUND_EFFECTS
            | BluetoothLeAudio.CONTEXT_TYPE_NOTIFICATIONS
            | BluetoothLeAudio.CONTEXT_TYPE_RINGTONE
            | BluetoothLeAudio.CONTEXT_TYPE_ALERTS
            | BluetoothLeAudio.CONTEXT_TYPE_EMERGENCY_ALARM;

    private BroadcastReceiver mBondStateChangedReceiver;
    private BroadcastReceiver mConnectionStateChangedReceiver;
    private BroadcastReceiver mMuteStateChangedReceiver;
@@ -320,8 +305,8 @@ public class LeAudioService extends ProfileService {
                Integer group_id = entry.getKey();
                if (descriptor.mIsActive) {
                    descriptor.mIsActive = false;
                    updateActiveDevices(group_id, descriptor.mActiveContexts,
                            ACTIVE_CONTEXTS_NONE, descriptor.mIsActive);
                    updateActiveDevices(group_id, descriptor.mDirection, AUDIO_DIRECTION_NONE,
                            descriptor.mIsActive);
                    break;
                }
            }
@@ -624,32 +609,6 @@ public class LeAudioService extends ProfileService {
        return result;
    }

    /**
     * Get supported group audio direction from available context.
     *
     * @param activeContexts bitset of active context to be matched with possible audio direction
     * support.
     * @return matched possible audio direction support masked bitset
     * {@link #AUDIO_DIRECTION_INPUT_BIT} if input audio is supported
     * {@link #AUDIO_DIRECTION_OUTPUT_BIT} if output audio is supported
     */
    private Integer getAudioDirectionsFromActiveContextsMap(Integer activeContexts) {
        Integer supportedAudioDirections = 0;

        if (((activeContexts & mContextSupportingInputAudio) != 0)
                || (Utils.isPtsTestMode()
                && (activeContexts
                & (BluetoothLeAudio.CONTEXT_TYPE_RINGTONE
                | BluetoothLeAudio.CONTEXT_TYPE_MEDIA)) != 0)) {
            supportedAudioDirections |= AUDIO_DIRECTION_INPUT_BIT;
        }
        if ((activeContexts & mContextSupportingOutputAudio) != 0) {
            supportedAudioDirections |= AUDIO_DIRECTION_OUTPUT_BIT;
        }

        return supportedAudioDirections;
    }

    private Integer getActiveGroupId() {
        synchronized (mGroupLock) {
            for (Map.Entry<Integer, LeAudioGroupDescriptor> entry : mGroupDescriptors.entrySet()) {
@@ -801,12 +760,7 @@ public class LeAudioService extends ProfileService {
    }

    private boolean updateActiveInDevice(BluetoothDevice device, Integer groupId,
            Integer oldActiveContexts, Integer newActiveContexts) {
        Integer oldSupportedAudioDirections =
                getAudioDirectionsFromActiveContextsMap(oldActiveContexts);
        Integer newSupportedAudioDirections =
                getAudioDirectionsFromActiveContextsMap(newActiveContexts);

            Integer oldSupportedAudioDirections, Integer newSupportedAudioDirections) {
        boolean oldSupportedByDeviceInput = (oldSupportedAudioDirections
                & AUDIO_DIRECTION_INPUT_BIT) != 0;
        boolean newSupportedByDeviceInput = (newSupportedAudioDirections
@@ -866,12 +820,7 @@ public class LeAudioService extends ProfileService {
    }

    private boolean updateActiveOutDevice(BluetoothDevice device, Integer groupId,
            Integer oldActiveContexts, Integer newActiveContexts) {
        Integer oldSupportedAudioDirections =
                getAudioDirectionsFromActiveContextsMap(oldActiveContexts);
        Integer newSupportedAudioDirections =
                getAudioDirectionsFromActiveContextsMap(newActiveContexts);

            Integer oldSupportedAudioDirections, Integer newSupportedAudioDirections) {
        boolean oldSupportedByDeviceOutput = (oldSupportedAudioDirections
                & AUDIO_DIRECTION_OUTPUT_BIT) != 0;
        boolean newSupportedByDeviceOutput = (newSupportedAudioDirections
@@ -941,13 +890,13 @@ public class LeAudioService extends ProfileService {
    /**
     * Report the active devices change to the active device manager and the media framework.
     * @param groupId id of group which devices should be updated
     * @param newActiveContexts new active contexts for group of devices
     * @param oldActiveContexts old active contexts for group of devices
     * @param newSupportedAudioDirections new supported audio directions for group of devices
     * @param oldSupportedAudioDirections old supported audio directions for group of devices
     * @param isActive if there is new active group
     * @return true if group is active after change false otherwise.
     */
    private boolean updateActiveDevices(Integer groupId, Integer oldActiveContexts,
            Integer newActiveContexts, boolean isActive) {
    private boolean updateActiveDevices(Integer groupId, Integer oldSupportedAudioDirections,
            Integer newSupportedAudioDirections, boolean isActive) {
        BluetoothDevice device = null;

        if (isActive) {
@@ -955,9 +904,11 @@ public class LeAudioService extends ProfileService {
        }

        boolean outReplaced =
                updateActiveOutDevice(device, groupId, oldActiveContexts, newActiveContexts);
                updateActiveOutDevice(device, groupId, oldSupportedAudioDirections,
                        newSupportedAudioDirections);
        boolean inReplaced =
                updateActiveInDevice(device, groupId, oldActiveContexts, newActiveContexts);
                updateActiveInDevice(device, groupId, oldSupportedAudioDirections,
                        newSupportedAudioDirections);

        if (outReplaced || inReplaced) {
            Intent intent = new Intent(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED);
@@ -1149,8 +1100,8 @@ public class LeAudioService extends ProfileService {
                return;
            }

            descriptor.mIsActive = updateActiveDevices(groupId,
                            ACTIVE_CONTEXTS_NONE, descriptor.mActiveContexts, true);
            descriptor.mIsActive = updateActiveDevices(groupId, AUDIO_DIRECTION_NONE,
                    descriptor.mDirection, true);

            if (descriptor.mIsActive) {
                notifyGroupStatusChanged(groupId, LeAudioStackEvent.GROUP_STATUS_ACTIVE);
@@ -1167,8 +1118,8 @@ public class LeAudioService extends ProfileService {
            }

            descriptor.mIsActive = false;
            updateActiveDevices(groupId, descriptor.mActiveContexts,
                    ACTIVE_CONTEXTS_NONE, descriptor.mIsActive);
            updateActiveDevices(groupId, descriptor.mDirection, AUDIO_DIRECTION_NONE,
                    descriptor.mIsActive);
            /* Clear lost devices */
            if (DBG) Log.d(TAG, "Clear for group: " + groupId);
            clearLostDevicesWhileStreaming(descriptor);
@@ -1342,13 +1293,13 @@ public class LeAudioService extends ProfileService {
            if (descriptor != null) {
                if (descriptor.mIsActive) {
                    descriptor.mIsActive =
                            updateActiveDevices(groupId, descriptor.mActiveContexts,
                                    available_contexts, descriptor.mIsActive);
                            updateActiveDevices(groupId, descriptor.mDirection, direction,
                            descriptor.mIsActive);
                    if (!descriptor.mIsActive) {
                        notifyGroupStatusChanged(groupId, BluetoothLeAudio.GROUP_STATUS_INACTIVE);
                    }
                }
                descriptor.mActiveContexts = available_contexts;
                descriptor.mDirection = direction;
            } else {
                Log.e(TAG, "no descriptors for group: " + groupId);
            }
@@ -1669,8 +1620,8 @@ public class LeAudioService extends ProfileService {
                    descriptor.mIsActive = false;
                    /* Update audio framework */
                    updateActiveDevices(myGroupId,
                            descriptor.mActiveContexts,
                            descriptor.mActiveContexts,
                            descriptor.mDirection,
                            descriptor.mDirection,
                            descriptor.mIsActive);
                    return;
                }
@@ -1678,8 +1629,8 @@ public class LeAudioService extends ProfileService {

            if (descriptor.mIsActive) {
                updateActiveDevices(myGroupId,
                        descriptor.mActiveContexts,
                        descriptor.mActiveContexts,
                        descriptor.mDirection,
                        descriptor.mDirection,
                        descriptor.mIsActive);
            }
        }
@@ -2803,7 +2754,7 @@ public class LeAudioService extends ProfileService {
            ProfileService.println(sb, "  Group: " + groupId);
            ProfileService.println(sb, "    isActive: " + descriptor.mIsActive);
            ProfileService.println(sb, "    isConnected: " + descriptor.mIsConnected);
            ProfileService.println(sb, "    mActiveContexts: " + descriptor.mActiveContexts);
            ProfileService.println(sb, "    mDirection: " + descriptor.mDirection);
            ProfileService.println(sb, "    group lead: " + getConnectedGroupLeadDevice(groupId));
            ProfileService.println(sb, "    first device: " + getFirstDeviceFromGroup(groupId));
            ProfileService.println(sb, "    lost lead device: "