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
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
index 59d8acb..8fd1910 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
@@ -49,6 +49,7 @@
  */
 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 @@
          *               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 @@
                 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:
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
index ba1dc64..6a4d650 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
@@ -35,6 +35,8 @@
 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 @@
 @RunWith(RobolectricTestRunner.class)
 public class BluetoothEventManagerTest {
 
+    private static final String DEVICE_NAME = "test_device_name";
+
     @Mock
     private LocalBluetoothAdapter mLocalAdapter;
     @Mock
@@ -71,6 +75,8 @@
     private BluetoothDevice mDevice2;
     @Mock
     private LocalBluetoothProfileManager mLocalProfileManager;
+    @Mock
+    private BluetoothUtils.ErrorListener mErrorListener;
 
     private Context mContext;
     private Intent mIntent;
@@ -92,6 +98,7 @@
 
         mCachedDevice1 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice1);
         mCachedDevice2 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice2);
+        BluetoothUtils.setErrorListener(mErrorListener);
     }
 
     @Test
@@ -344,4 +351,80 @@
         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));
+    }
 }