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

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

Merge "Revert "Revert "Pass in active device to all BluetoothA2dp APIs ..."" into rvc-dev

parents 64fff70a 3e0f661f
Loading
Loading
Loading
Loading
+20 −5
Original line number Diff line number Diff line
@@ -83,7 +83,11 @@ public abstract class AbstractBluetoothA2dpPreferenceController extends
        final BluetoothCodecConfig codecConfig = mBluetoothA2dpConfigStore.createCodecConfig();
        synchronized (mBluetoothA2dpConfigStore) {
            if (mBluetoothA2dp != null) {
                setCodecConfigPreference(null, codecConfig);    // Use current active device
                BluetoothDevice activeDevice = mBluetoothA2dp.getActiveDevice();
                if (activeDevice == null) {
                    return false;
                }
                setCodecConfigPreference(activeDevice, codecConfig);
            }
        }
        // Because the setting is not persisted into permanent storage, we cannot call update state
@@ -102,13 +106,14 @@ public abstract class AbstractBluetoothA2dpPreferenceController extends

    @Override
    public void updateState(Preference preference) {
        if (getCodecConfig(null) == null || mPreference == null) { // Use current active device
        BluetoothDevice activeDevice = mBluetoothA2dp.getActiveDevice();
        if (activeDevice == null || getCodecConfig(activeDevice) == null || mPreference == null) {
            return;
        }

        BluetoothCodecConfig codecConfig;
        synchronized (mBluetoothA2dpConfigStore) {
            codecConfig = getCodecConfig(null);         // Use current active device
            codecConfig = getCodecConfig(activeDevice);
        }

        final int index = getCurrentA2dpSettingIndex(codecConfig);
@@ -178,13 +183,23 @@ public abstract class AbstractBluetoothA2dpPreferenceController extends
    @VisibleForTesting
    void setCodecConfigPreference(BluetoothDevice device,
            BluetoothCodecConfig config) {
        mBluetoothA2dp.setCodecConfigPreference(device, config);
        BluetoothDevice bluetoothDevice =
                (device != null) ? device : mBluetoothA2dp.getActiveDevice();
        if (bluetoothDevice == null) {
            return;
        }
        mBluetoothA2dp.setCodecConfigPreference(bluetoothDevice, config);
    }

    @VisibleForTesting
    BluetoothCodecConfig getCodecConfig(BluetoothDevice device) {
        if (mBluetoothA2dp != null) {
            BluetoothCodecStatus codecStatus = mBluetoothA2dp.getCodecStatus(device);
            BluetoothDevice bluetoothDevice =
                    (device != null) ? device : mBluetoothA2dp.getActiveDevice();
            if (bluetoothDevice == null) {
                return null;
            }
            BluetoothCodecStatus codecStatus = mBluetoothA2dp.getCodecStatus(bluetoothDevice);
            if (codecStatus != null) {
                return codecStatus.getCodecConfig();
            }
+23 −4
Original line number Diff line number Diff line
@@ -80,7 +80,10 @@ public abstract class AbstractBluetoothDialogPreferenceController extends
        }
        writeConfigurationValues(index);
        final BluetoothCodecConfig codecConfig = mBluetoothA2dpConfigStore.createCodecConfig();
        bluetoothA2dp.setCodecConfigPreference(null, codecConfig);
        BluetoothDevice activeDevice = mBluetoothA2dp.getActiveDevice();
        if (activeDevice != null) {
            bluetoothA2dp.setCodecConfigPreference(activeDevice, codecConfig);
        }
        mPreference.setSummary(((BaseBluetoothDialogPreference) mPreference).generateSummary(
                index));
    }
@@ -146,7 +149,13 @@ public abstract class AbstractBluetoothDialogPreferenceController extends
        if (bluetoothA2dp == null) {
            return null;
        }
        final BluetoothCodecStatus codecStatus = bluetoothA2dp.getCodecStatus(null);
        BluetoothDevice activeDevice = bluetoothA2dp.getActiveDevice();
        if (activeDevice == null) {
            Log.d(TAG, "Unable to get current codec config. No active device.");
            return null;
        }
        final BluetoothCodecStatus codecStatus =
                bluetoothA2dp.getCodecStatus(activeDevice);
        if (codecStatus == null) {
            Log.d(TAG, "Unable to get current codec config. Codec status is null");
            return null;
@@ -164,7 +173,12 @@ public abstract class AbstractBluetoothDialogPreferenceController extends
        if (bluetoothA2dp == null) {
            return null;
        }
        final BluetoothCodecStatus codecStatus = bluetoothA2dp.getCodecStatus(device);
        BluetoothDevice bluetoothDevice =
                (device != null) ? device : bluetoothA2dp.getActiveDevice();
        if (bluetoothDevice == null) {
            return null;
        }
        final BluetoothCodecStatus codecStatus = bluetoothA2dp.getCodecStatus(bluetoothDevice);
        if (codecStatus != null) {
            return codecStatus.getCodecsSelectableCapabilities();
        }
@@ -177,7 +191,12 @@ public abstract class AbstractBluetoothDialogPreferenceController extends
     * @return {@link BluetoothCodecConfig}.
     */
    protected BluetoothCodecConfig getSelectableByCodecType(int codecTypeValue) {
        final BluetoothCodecConfig[] configs = getSelectableConfigs(null);
        BluetoothDevice activeDevice = mBluetoothA2dp.getActiveDevice();
        if (activeDevice == null) {
            Log.d(TAG, "Unable to get selectable config. No active device.");
            return null;
        }
        final BluetoothCodecConfig[] configs = getSelectableConfigs(activeDevice);
        if (configs == null) {
            Log.d(TAG, "Unable to get selectable config. Selectable configs is empty.");
            return null;
+4 −3
Original line number Diff line number Diff line
@@ -83,9 +83,9 @@ public class BluetoothCodecDialogPreferenceController extends
            return index;
        }
        // Check HD audio is enabled, display the available list.
        if (bluetoothA2dp.getOptionalCodecsEnabled(activeDevice)
        if (bluetoothA2dp.isOptionalCodecsEnabled(activeDevice)
                == BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED) {
            BluetoothCodecConfig[] configs = getSelectableConfigs(null);
            BluetoothCodecConfig[] configs = getSelectableConfigs(activeDevice);
            if (configs != null) {
                return getIndexFromConfig(configs);
            }
@@ -101,7 +101,8 @@ public class BluetoothCodecDialogPreferenceController extends
        int codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
        switch (index) {
            case 0:
                codecTypeValue = getHighestCodec(getSelectableConfigs(null));
                codecTypeValue = getHighestCodec(getSelectableConfigs(
                    mBluetoothA2dp.getActiveDevice()));
                codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST;
                break;
            case 1:
+10 −5
Original line number Diff line number Diff line
@@ -58,11 +58,11 @@ public class BluetoothHDAudioPreferenceController extends AbstractBluetoothPrefe
            mPreference.setEnabled(false);
            return;
        }
        final boolean supported = (bluetoothA2dp.supportsOptionalCodecs(activeDevice)
        final boolean supported = (bluetoothA2dp.isOptionalCodecsSupported(activeDevice)
                == BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
        mPreference.setEnabled(supported);
        if (supported) {
            final boolean isEnabled = bluetoothA2dp.getOptionalCodecsEnabled(activeDevice)
            final boolean isEnabled = bluetoothA2dp.isOptionalCodecsEnabled(activeDevice)
                    == BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED;
            ((SwitchPreference) mPreference).setChecked(isEnabled);
        }
@@ -84,11 +84,16 @@ public class BluetoothHDAudioPreferenceController extends AbstractBluetoothPrefe
        final int prefValue = enabled
                ? BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED
                : BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED;
        bluetoothA2dp.setOptionalCodecsEnabled(bluetoothA2dp.getActiveDevice(), prefValue);
        BluetoothDevice activeDevice = bluetoothA2dp.getActiveDevice();
        if (activeDevice == null) {
            mPreference.setEnabled(false);
            return true;
        }
        bluetoothA2dp.setOptionalCodecsEnabled(activeDevice, prefValue);
        if (enabled) {
            bluetoothA2dp.enableOptionalCodecs(null); // Use current active device
            bluetoothA2dp.enableOptionalCodecs(activeDevice);
        } else {
            bluetoothA2dp.disableOptionalCodecs(null); // Use current active device
            bluetoothA2dp.disableOptionalCodecs(activeDevice);
        }
        mCallback.onBluetoothHDAudioEnabled(enabled);
        return true;
+21 −8
Original line number Diff line number Diff line
@@ -24,8 +24,10 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCodecConfig;
import android.bluetooth.BluetoothCodecStatus;
import android.bluetooth.BluetoothDevice;
import android.content.Context;

import androidx.lifecycle.LifecycleOwner;
@@ -49,6 +51,7 @@ import java.util.List;
public class AbstractBluetoothDialogPreferenceControllerTest {

    private static final String SUMMARY = "Test summary";
    private static final String DEVICE_ADDRESS = "00:11:22:33:44:55";

    @Mock
    private BluetoothA2dp mBluetoothA2dp;
@@ -62,6 +65,7 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
    private BluetoothCodecConfig mCodecConfigAAC;
    private BluetoothCodecConfig mCodecConfigSBC;
    private BluetoothCodecConfig[] mCodecConfigs = new BluetoothCodecConfig[2];
    private BluetoothDevice mActiveDevice;
    private Context mContext;
    private int mCurrentConfig;
    private LifecycleOwner mLifecycleOwner;
@@ -74,6 +78,7 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
        mLifecycleOwner = () -> mLifecycle;
        mLifecycle = new Lifecycle(mLifecycleOwner);
        mBluetoothA2dpConfigStore = spy(new BluetoothA2dpConfigStore());
        mActiveDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(DEVICE_ADDRESS);
        mController = spy(new AbstractBluetoothDialogPreferenceControllerImpl(mContext, mLifecycle,
                mBluetoothA2dpConfigStore));
        mPreference = spy(new BaseBluetoothDialogPreferenceImpl(mContext));
@@ -87,6 +92,7 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
        mController.displayPreference(mScreen);
        mCurrentConfig = mController.getCurrentConfigIndex();
        when(mPreference.generateSummary(mCurrentConfig)).thenReturn(SUMMARY);
        when(mBluetoothA2dp.getActiveDevice()).thenReturn(mActiveDevice);
    }

    @Test
@@ -103,13 +109,15 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
    @Test
    public void onIndexUpdated_checkFlow() {
        mCodecStatus = new BluetoothCodecStatus(mCodecConfigAAC, null, null);
        when(mBluetoothA2dp.getCodecStatus(null)).thenReturn(mCodecStatus);
        when(mBluetoothA2dp.getCodecStatus(
            mActiveDevice)).thenReturn(mCodecStatus);
        when(mBluetoothA2dpConfigStore.createCodecConfig()).thenReturn(mCodecConfigAAC);
        mController.onBluetoothServiceConnected(mBluetoothA2dp);
        mController.onIndexUpdated(mCurrentConfig);

        verify(mController).writeConfigurationValues(mCurrentConfig);
        verify(mBluetoothA2dp).setCodecConfigPreference(null, mCodecConfigAAC);
        verify(mBluetoothA2dp).setCodecConfigPreference(
                mActiveDevice, mCodecConfigAAC);
        assertThat(mPreference.getSummary()).isEqualTo(SUMMARY);
    }

@@ -134,14 +142,15 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
        assertThat(mController.getCurrentCodecConfig()).isNull();

        mController.onBluetoothServiceConnected(mBluetoothA2dp);
        when(mBluetoothA2dp.getCodecStatus(null)).thenReturn(null);
        when(mBluetoothA2dp.getCodecStatus(mActiveDevice)).thenReturn(null);
        assertThat(mController.getCurrentCodecConfig()).isNull();
    }

    @Test
    public void getCurrentCodecConfig_verifyConfig() {
        mCodecStatus = new BluetoothCodecStatus(mCodecConfigAAC, null, null);
        when(mBluetoothA2dp.getCodecStatus(null)).thenReturn(mCodecStatus);
        when(mBluetoothA2dp.getCodecStatus(
            mActiveDevice)).thenReturn(mCodecStatus);
        mController.onBluetoothServiceConnected(mBluetoothA2dp);

        assertThat(mController.getCurrentCodecConfig()).isEqualTo(mCodecConfigAAC);
@@ -150,7 +159,8 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
    @Test
    public void getSelectableConfigs_verifyConfig() {
        mCodecStatus = new BluetoothCodecStatus(mCodecConfigAAC, null, mCodecConfigs);
        when(mBluetoothA2dp.getCodecStatus(null)).thenReturn(mCodecStatus);
        when(mBluetoothA2dp.getCodecStatus(
            mActiveDevice)).thenReturn(mCodecStatus);
        mController.onBluetoothServiceConnected(mBluetoothA2dp);

        assertThat(mController.getSelectableConfigs(null)).isEqualTo(mCodecConfigs);
@@ -159,7 +169,8 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
    @Test
    public void getSelectableByCodecType_verifyConfig() {
        mCodecStatus = new BluetoothCodecStatus(mCodecConfigAAC, null, mCodecConfigs);
        when(mBluetoothA2dp.getCodecStatus(null)).thenReturn(mCodecStatus);
        when(mBluetoothA2dp.getCodecStatus(
            mActiveDevice)).thenReturn(mCodecStatus);
        mController.onBluetoothServiceConnected(mBluetoothA2dp);

        assertThat(mController.getSelectableByCodecType(BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC))
@@ -169,7 +180,8 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
    @Test
    public void getSelectableByCodecType_unavailable() {
        mCodecStatus = new BluetoothCodecStatus(mCodecConfigAAC, null, mCodecConfigs);
        when(mBluetoothA2dp.getCodecStatus(null)).thenReturn(mCodecStatus);
        when(mBluetoothA2dp.getCodecStatus(
            mActiveDevice)).thenReturn(mCodecStatus);
        mController.onBluetoothServiceConnected(mBluetoothA2dp);

        assertThat(mController.getSelectableByCodecType(
@@ -179,7 +191,8 @@ public class AbstractBluetoothDialogPreferenceControllerTest {
    @Test
    public void onBluetoothServiceConnected_verifyBluetoothA2dpConfigStore() {
        mCodecStatus = new BluetoothCodecStatus(mCodecConfigAAC, null, mCodecConfigs);
        when(mBluetoothA2dp.getCodecStatus(null)).thenReturn(mCodecStatus);
        when(mBluetoothA2dp.getCodecStatus(
            mActiveDevice)).thenReturn(mCodecStatus);
        mController.onBluetoothServiceConnected(mBluetoothA2dp);

        verify(mBluetoothA2dpConfigStore).setCodecType(mCodecConfigAAC.getCodecType());
Loading