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

Commit b82f290f authored by Yiyi Shen's avatar Yiyi Shen
Browse files

[Audiosharing] Set earlist connected device active in sharing.

Flagged with enable_le_audio_sharing

Bug: 305620450
Test: Manual
Change-Id: I8e0132f3d982fe2723581cd2aafd3cb9a9ed409c
parent 2dfae393
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -160,6 +160,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
                                    + ", reason = "
                                    + reason);
                    mBluetoothDeviceUpdater.forceUpdate();
                    AudioSharingUtils.updateActiveDeviceIfNeeded(mLocalBtManager);
                }

                @Override
@@ -204,6 +205,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
                                    + ", reason = "
                                    + reason);
                    mBluetoothDeviceUpdater.forceUpdate();
                    AudioSharingUtils.updateActiveDeviceIfNeeded(mLocalBtManager);
                }

                @Override
@@ -299,7 +301,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
        mPreferenceGroup.setVisible(false);
        mAudioSharingSettingsPreference.setVisible(false);

        if (isAvailable() && mBluetoothDeviceUpdater != null) {
        if (isAvailable()) {
            mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
            mBluetoothDeviceUpdater.forceUpdate();
        }
@@ -309,6 +311,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
    public int getAvailabilityStatus() {
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
                        && Flags.enableLeAudioSharing()
                        && mBluetoothDeviceUpdater != null
                ? AVAILABLE_UNSEARCHABLE
                : UNSUPPORTED_ON_DEVICE;
    }
+1 −0
Original line number Diff line number Diff line
@@ -192,6 +192,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                                    + sourceId
                                    + ", reason = "
                                    + reason);
                    AudioSharingUtils.updateActiveDeviceIfNeeded(mBtManager);
                }

                @Override
+31 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.connecteddevice.audiosharing;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
@@ -89,11 +90,11 @@ public class AudioSharingUtils {
     * @return A list of ordered connected devices eligible for the audio sharing. The active device
     *     is placed in the first place if it exists.
     */
    public static ArrayList<CachedBluetoothDevice> buildOrderedConnectedLeadDevices(
    public static List<CachedBluetoothDevice> buildOrderedConnectedLeadDevices(
            LocalBluetoothManager localBtManager,
            Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices,
            boolean filterByInSharing) {
        ArrayList<CachedBluetoothDevice> orderedDevices = new ArrayList<>();
        List<CachedBluetoothDevice> orderedDevices = new ArrayList<>();
        LocalBluetoothLeBroadcastAssistant assistant =
                localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
        if (assistant == null) return orderedDevices;
@@ -234,4 +235,32 @@ public class AudioSharingUtils {
        ThreadUtils.postOnMainThread(
                () -> Toast.makeText(context, message, Toast.LENGTH_LONG).show());
    }

    /** Automatically update active device if needed. */
    public static void updateActiveDeviceIfNeeded(LocalBluetoothManager localBtManager) {
        if (localBtManager == null) return;
        Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices =
                fetchConnectedDevicesByGroupId(localBtManager);
        List<CachedBluetoothDevice> devicesInSharing =
                buildOrderedConnectedLeadDevices(
                        localBtManager, groupedConnectedDevices, /* filterByInSharing= */ true);
        if (devicesInSharing.isEmpty()) return;
        List<BluetoothDevice> devices =
                BluetoothAdapter.getDefaultAdapter().getMostRecentlyConnectedDevices();
        CachedBluetoothDevice targetDevice = null;
        int targetDeviceIdx = -1;
        for (CachedBluetoothDevice device : devicesInSharing) {
            if (devices.contains(device.getDevice())) {
                int idx = devices.indexOf(device.getDevice());
                if (idx > targetDeviceIdx) {
                    targetDeviceIdx = idx;
                    targetDevice = device;
                }
            }
        }
        if (targetDevice != null && !isActiveLeAudioDevice(targetDevice)) {
            Log.d(TAG, "Set active device: " + targetDevice.getDevice().getAnonymizedAddress());
            targetDevice.setActive();
        }
    }
}