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

Commit 6f2ca821 authored by Haijie Hong's avatar Haijie Hong
Browse files

Check connection policy before showing "Problem connecting"

Bug: 396558715
Test: atest CachedBluetoothDevice
Flag: EXEMPT minor fix
Change-Id: I5842ee0d7ae66c780b4aec0001e36b360c8aa2cb
parent 40aa32b3
Loading
Loading
Loading
Loading
+25 −4
Original line number Diff line number Diff line
@@ -1775,10 +1775,31 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
                + " mIsLeAudioProfileConnectedFail=" + mIsLeAudioProfileConnectedFail
                + " mIsHeadsetProfileConnectedFail=" + mIsHeadsetProfileConnectedFail
                + " isConnectedSapDevice()=" + isConnectedSapDevice());

        return mIsA2dpProfileConnectedFail || mIsHearingAidProfileConnectedFail
                || (!isConnectedSapDevice() && mIsHeadsetProfileConnectedFail)
                || mIsLeAudioProfileConnectedFail;
        if (mIsA2dpProfileConnectedFail) {
            A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
            if (a2dpProfile != null && a2dpProfile.isEnabled(mDevice)) {
                return true;
            }
        }
        if (mIsHearingAidProfileConnectedFail) {
            HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
            if (hearingAidProfile != null && hearingAidProfile.isEnabled(mDevice)) {
                return true;
            }
        }
        if (!isConnectedSapDevice() && mIsHeadsetProfileConnectedFail) {
            HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile();
            if (headsetProfile != null && headsetProfile.isEnabled(mDevice)) {
                return true;
            }
        }
        if (mIsLeAudioProfileConnectedFail) {
            LeAudioProfile leAudioProfile = mProfileManager.getLeAudioProfile();
            if (leAudioProfile != null && leAudioProfile.isEnabled(mDevice)) {
                return true;
            }
        }
        return false;
    }

    /**
+19 −0
Original line number Diff line number Diff line
@@ -174,6 +174,8 @@ public class CachedBluetoothDeviceTest {
        updateProfileStatus(connectingProfile, BluetoothProfile.STATE_CONNECTING);
        // Set connection policy
        when(connectingProfile.getConnectionPolicy(mDevice)).thenReturn(connectionPolicy);
        when(connectingProfile.isEnabled(mDevice))
                .thenReturn(connectionPolicy > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);

        // Act & Assert:
        //   Get the expected connection summary.
@@ -183,6 +185,9 @@ public class CachedBluetoothDeviceTest {

    @Test
    public void onProfileStateChanged_testConnectingToDisconnected_policyAllowed_problem() {
        when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
        when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);

        String connectTimeoutString = mContext.getString(R.string.profile_connect_timeout_subtext);

        testTransitionFromConnectingToDisconnected(mA2dpProfile, mLeAudioProfile,
@@ -197,6 +202,9 @@ public class CachedBluetoothDeviceTest {

    @Test
    public void onProfileStateChanged_testConnectingToDisconnected_policyForbidden_noProblem() {
        when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
        when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);

        testTransitionFromConnectingToDisconnected(mA2dpProfile, mLeAudioProfile,
        BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, null);
        testTransitionFromConnectingToDisconnected(mHearingAidProfile, mLeAudioProfile,
@@ -209,6 +217,9 @@ public class CachedBluetoothDeviceTest {

    @Test
    public void onProfileStateChanged_testConnectingToDisconnected_policyUnknown_noProblem() {
        when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
        when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);

        testTransitionFromConnectingToDisconnected(mA2dpProfile, mLeAudioProfile,
        BluetoothProfile.CONNECTION_POLICY_UNKNOWN, null);
        testTransitionFromConnectingToDisconnected(mHearingAidProfile, mLeAudioProfile,
@@ -1822,6 +1833,10 @@ public class CachedBluetoothDeviceTest {
    @Test
    public void getConnectionSummary_profileConnectedFail_showErrorMessage() {
        final A2dpProfile profile = mock(A2dpProfile.class);
        when(mProfileManager.getA2dpProfile()).thenReturn(profile);
        when(profile.getConnectionPolicy(mDevice))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        when(profile.isEnabled(mDevice)).thenReturn(true);
        mCachedDevice.onProfileStateChanged(profile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice.setProfileConnectedStatus(BluetoothProfile.A2DP, true);

@@ -1834,6 +1849,10 @@ public class CachedBluetoothDeviceTest {
    @Test
    public void getTvConnectionSummary_profileConnectedFail_showErrorMessage() {
        final A2dpProfile profile = mock(A2dpProfile.class);
        when(mProfileManager.getA2dpProfile()).thenReturn(profile);
        when(profile.getConnectionPolicy(mDevice))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        when(profile.isEnabled(mDevice)).thenReturn(true);
        mCachedDevice.onProfileStateChanged(profile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice.setProfileConnectedStatus(BluetoothProfile.A2DP, true);