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

Commit a76e18d1 authored by Hugh Chen's avatar Hugh Chen
Browse files

Fix toasts message is not displayed when cancel BT pairing request

Before this CL, Bluetooth EventManager did not handle the error
reason that was called UNBOND_REASON_REMOVED. If UNBOND_REASON_REMOVED
is sended when cancel BT pairing, the error toast will not display.

This CL will show error toast when received UNBOND_REASON_REMOVED.

Bug: 173165769
Test: make -j42 RunSettingsLibRoboTests
Change-Id: I5c75c17ebe204c8b9e5f1ff18c39ef8142e009eb
parent a2402d95
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
 */
public class BluetoothEventManager {
    private static final String TAG = "BluetoothEventManager";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    private final LocalBluetoothAdapter mLocalAdapter;
    private final CachedBluetoothDeviceManager mDeviceManager;
@@ -366,6 +367,9 @@ public class BluetoothEventManager {
         *               BluetoothDevice.UNBOND_REASON_*
         */
        private void showUnbondMessage(Context context, String name, int reason) {
            if (DEBUG) {
                Log.d(TAG, "showUnbondMessage() name : " + name + ", reason : " + reason);
            }
            int errorMsg;

            switch (reason) {
@@ -382,6 +386,7 @@ public class BluetoothEventManager {
                case BluetoothDevice.UNBOND_REASON_AUTH_TIMEOUT:
                case BluetoothDevice.UNBOND_REASON_REPEATED_ATTEMPTS:
                case BluetoothDevice.UNBOND_REASON_REMOTE_AUTH_CANCELED:
                case BluetoothDevice.UNBOND_REASON_REMOVED:
                    errorMsg = R.string.bluetooth_pairing_error_message;
                    break;
                default:
+83 −0
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ import android.content.IntentFilter;
import android.os.UserHandle;
import android.telephony.TelephonyManager;

import com.android.settingslib.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -49,6 +51,8 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class BluetoothEventManagerTest {

    private static final String DEVICE_NAME = "test_device_name";

    @Mock
    private LocalBluetoothAdapter mLocalAdapter;
    @Mock
@@ -71,6 +75,8 @@ public class BluetoothEventManagerTest {
    private BluetoothDevice mDevice2;
    @Mock
    private LocalBluetoothProfileManager mLocalProfileManager;
    @Mock
    private BluetoothUtils.ErrorListener mErrorListener;

    private Context mContext;
    private Intent mIntent;
@@ -92,6 +98,7 @@ public class BluetoothEventManagerTest {

        mCachedDevice1 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice1);
        mCachedDevice2 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice2);
        BluetoothUtils.setErrorListener(mErrorListener);
    }

    @Test
@@ -344,4 +351,80 @@ public class BluetoothEventManagerTest {
        assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.HEADSET)).isFalse();
        assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.HEARING_AID)).isFalse();
    }

    @Test
    public void showUnbondMessage_reasonRemoved_showCorrectedErrorCode() {
        mIntent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
        mIntent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
        mIntent.putExtra(BluetoothDevice.EXTRA_REASON, BluetoothDevice.UNBOND_REASON_REMOVED);
        when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedDevice1);
        when(mCachedDevice1.getName()).thenReturn(DEVICE_NAME);

        mContext.sendBroadcast(mIntent);

        verify(mErrorListener).onShowError(any(Context.class), eq(DEVICE_NAME),
                eq(R.string.bluetooth_pairing_error_message));
    }

    @Test
    public void showUnbondMessage_reasonAuthTimeout_showCorrectedErrorCode() {
        mIntent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
        mIntent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
        mIntent.putExtra(BluetoothDevice.EXTRA_REASON, BluetoothDevice.UNBOND_REASON_AUTH_TIMEOUT);
        when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedDevice1);
        when(mCachedDevice1.getName()).thenReturn(DEVICE_NAME);

        mContext.sendBroadcast(mIntent);

        verify(mErrorListener).onShowError(any(Context.class), eq(DEVICE_NAME),
                eq(R.string.bluetooth_pairing_error_message));
    }

    @Test
    public void showUnbondMessage_reasonRemoteDeviceDown_showCorrectedErrorCode() {
        mIntent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
        mIntent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
        mIntent.putExtra(BluetoothDevice.EXTRA_REASON,
                BluetoothDevice.UNBOND_REASON_REMOTE_DEVICE_DOWN);
        when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedDevice1);
        when(mCachedDevice1.getName()).thenReturn(DEVICE_NAME);

        mContext.sendBroadcast(mIntent);

        verify(mErrorListener).onShowError(any(Context.class), eq(DEVICE_NAME),
                eq(R.string.bluetooth_pairing_device_down_error_message));
    }

    @Test
    public void showUnbondMessage_reasonAuthRejected_showCorrectedErrorCode() {
        mIntent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
        mIntent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
        mIntent.putExtra(BluetoothDevice.EXTRA_REASON, BluetoothDevice.UNBOND_REASON_AUTH_REJECTED);
        when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedDevice1);
        when(mCachedDevice1.getName()).thenReturn(DEVICE_NAME);

        mContext.sendBroadcast(mIntent);

        verify(mErrorListener).onShowError(any(Context.class), eq(DEVICE_NAME),
                eq(R.string.bluetooth_pairing_rejected_error_message));
    }

    @Test
    public void showUnbondMessage_reasonAuthFailed_showCorrectedErrorCode() {
        mIntent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
        mIntent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
        mIntent.putExtra(BluetoothDevice.EXTRA_REASON, BluetoothDevice.UNBOND_REASON_AUTH_FAILED);
        when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedDevice1);
        when(mCachedDevice1.getName()).thenReturn(DEVICE_NAME);

        mContext.sendBroadcast(mIntent);

        verify(mErrorListener).onShowError(any(Context.class), eq(DEVICE_NAME),
                eq(R.string.bluetooth_pairing_pin_error_message));
    }
}