summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Hugh Chen <hughchen@google.com> 2020-11-17 14:09:08 +0800
committer Hugh Chen <hughchen@google.com> 2020-11-26 14:54:50 +0800
commita76e18d1da516bc35c6f7fdb2c677f1318894fc1 (patch)
tree781873fb4d45bdeb6c1a5b8bdfea957f672dd0a6
parenta2402d954c550bc2c20973c7a8dc0857bd697aab (diff)
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
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java5
-rw-r--r--packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java83
2 files changed, 88 insertions, 0 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
index 59d8acb82196..8fd1910041c8 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 @@ 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:
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 ba1dc64ce1e6..6a4d650ebf31 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.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));
+ }
}