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

Commit a2840db8 authored by chelseahao's avatar chelseahao
Browse files

Check if there's any active device or device with source before entering audio streams page.

There could be cases where there's only connected but no active devices.

Flag: com.android.settingslib.flags.enable_le_audio_sharing
Test: atest
Bug: 412565521
Change-Id: I6ea51626af6d838ef8a44c0cf31f3c638506daec
parent 5372c42f
Loading
Loading
Loading
Loading
+6 −12
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import static com.android.settingslib.bluetooth.BluetoothBroadcastUtils.SCHEME_B
import android.app.Activity;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.content.Context;
import android.content.Intent;
@@ -42,7 +41,7 @@ import com.android.settings.core.SubSettingLauncher;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.utils.ThreadUtils;

public class AudioStreamConfirmDialog extends InstrumentedDialogFragment {
@@ -58,7 +57,7 @@ public class AudioStreamConfirmDialog extends InstrumentedDialogFragment {
    @Nullable
    private BluetoothLeBroadcastMetadata mBroadcastMetadata;
    @Nullable
    private BluetoothDevice mConnectedDevice;
    private CachedBluetoothDevice mConnectedDevice;
    private int mAudioStreamConfirmDialogId = SettingsEnums.PAGE_UNKNOWN;

    @Override
@@ -281,23 +280,18 @@ public class AudioStreamConfirmDialog extends InstrumentedDialogFragment {
    }

    @Nullable
    private BluetoothDevice getConnectedDevice() {
    private CachedBluetoothDevice getConnectedDevice() {
        var localBluetoothManager = Utils.getLocalBluetoothManager(getActivity());
        if (localBluetoothManager == null) {
            return null;
        }
        LocalBluetoothLeBroadcastAssistant assistant =
                localBluetoothManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
        if (assistant == null) {
            return null;
        }
        var devices = assistant.getAllConnectedDevices();
        return devices.isEmpty() ? null : devices.get(0);
        return AudioStreamsHelper.getCachedBluetoothDeviceInSharingOrLeConnected(
                localBluetoothManager).orElse(null);
    }

    private String getConnectedDeviceName() {
        if (mConnectedDevice != null) {
            String alias = mConnectedDevice.getAlias();
            String alias = mConnectedDevice.getName();
            return TextUtils.isEmpty(alias) ? getString(DEFAULT_DEVICE_NAME) : alias;
        }
        Log.w(TAG, "getConnectedDeviceName : no connected device!");
+18 −25
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothStatusCodes;
import android.content.ComponentName;
import android.content.Context;
@@ -50,6 +49,7 @@ import com.android.settings.R;
import com.android.settings.connecteddevice.audiosharing.audiostreams.testshadows.ShadowAudioStreamsHelper;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -70,9 +70,6 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
@Config(
        shadows = {
@@ -104,7 +101,7 @@ public class AudioStreamConfirmDialogTest {
    @Mock
    private VolumeControlProfile mVolumeControl;
    @Mock
    private BluetoothDevice mBluetoothDevice;
    private CachedBluetoothDevice mCachedBluetoothDevice;
    private AudioStreamConfirmDialog mDialogFragment;

    @Before
@@ -133,6 +130,7 @@ public class AudioStreamConfirmDialogTest {
    @After
    public void tearDown() {
        ShadowBluetoothUtils.reset();
        ShadowAudioStreamsHelper.reset();
        mDialogFragment.dismiss();
    }

@@ -231,10 +229,9 @@ public class AudioStreamConfirmDialogTest {

    @Test
    public void showDialog_noMetadata() {
        List<BluetoothDevice> devices = new ArrayList<>();
        devices.add(mBluetoothDevice);
        when(mAssistant.getAllConnectedDevices()).thenReturn(devices);
        when(mBluetoothDevice.getAlias()).thenReturn(DEVICE_NAME);
        ShadowAudioStreamsHelper.setCachedBluetoothDeviceInSharingOrLeConnected(
                mCachedBluetoothDevice);
        when(mCachedBluetoothDevice.getName()).thenReturn(DEVICE_NAME);

        FragmentController.setupFragment(
                mDialogFragment,
@@ -279,10 +276,9 @@ public class AudioStreamConfirmDialogTest {

    @Test
    public void showDialog_invalidMetadata() {
        List<BluetoothDevice> devices = new ArrayList<>();
        devices.add(mBluetoothDevice);
        when(mAssistant.getAllConnectedDevices()).thenReturn(devices);
        when(mBluetoothDevice.getAlias()).thenReturn(DEVICE_NAME);
        ShadowAudioStreamsHelper.setCachedBluetoothDeviceInSharingOrLeConnected(
                mCachedBluetoothDevice);
        when(mCachedBluetoothDevice.getName()).thenReturn(DEVICE_NAME);

        Intent intent = new Intent();
        intent.putExtra(KEY_BROADCAST_METADATA, "invalid");
@@ -330,10 +326,9 @@ public class AudioStreamConfirmDialogTest {

    @Test
    public void showDialog_confirmListen() {
        List<BluetoothDevice> devices = new ArrayList<>();
        devices.add(mBluetoothDevice);
        when(mAssistant.getAllConnectedDevices()).thenReturn(devices);
        when(mBluetoothDevice.getAlias()).thenReturn("");
        ShadowAudioStreamsHelper.setCachedBluetoothDeviceInSharingOrLeConnected(
                mCachedBluetoothDevice);
        when(mCachedBluetoothDevice.getName()).thenReturn("");

        Intent intent = new Intent();
        intent.putExtra(KEY_BROADCAST_METADATA, VALID_METADATA);
@@ -389,10 +384,9 @@ public class AudioStreamConfirmDialogTest {

    @Test
    public void showDialog_turnOffTalkback() {
        List<BluetoothDevice> devices = new ArrayList<>();
        devices.add(mBluetoothDevice);
        when(mAssistant.getAllConnectedDevices()).thenReturn(devices);
        when(mBluetoothDevice.getAlias()).thenReturn("");
        ShadowAudioStreamsHelper.setCachedBluetoothDeviceInSharingOrLeConnected(
                mCachedBluetoothDevice);
        when(mCachedBluetoothDevice.getName()).thenReturn("");
        ShadowAudioStreamsHelper.setEnabledScreenReaderService(new ComponentName("pkg", "class"));

        Intent intent = new Intent();
@@ -449,10 +443,9 @@ public class AudioStreamConfirmDialogTest {

    @Test
    public void showDialog_getDataStringFromIntent_confirmListen() {
        List<BluetoothDevice> devices = new ArrayList<>();
        devices.add(mBluetoothDevice);
        when(mAssistant.getAllConnectedDevices()).thenReturn(devices);
        when(mBluetoothDevice.getAlias()).thenReturn("");
        ShadowAudioStreamsHelper.setCachedBluetoothDeviceInSharingOrLeConnected(
                mCachedBluetoothDevice);
        when(mCachedBluetoothDevice.getName()).thenReturn("");

        Intent intent = new Intent();
        intent.setData(Uri.parse(VALID_METADATA_LOWERCASE));